[comp.os.minix] convert od output back to a file. A simple file/disk patcher.

hcj@lzaz.ATT.COM (HC Johnson) (03/03/89)

I use this program to patch a file or disk using 'od' to read the
information first.  This shar contains 2 files, tofile.c, and tofile.doc.
Patching a disk is done at the users risk.

Howard C. Johnson
ATT Bell Labs
att!lzaz!hcj
hcj@lzaz.att.com

========================cut here====================
echo x - tofile.c
gres '^X' '' > tofile.c << '/'
X/*
X * tofile.c restore a binary file from a od -x
X */
X/* I use this to patch the hard disk boot block, /dev/rhd0.
X * Use od -x /dev/rhd0 to get the hex dump.
X * Use mined or vi to edit it to change partitions or names
X * Then use tofile /dev/rhd0 < file to rewrite it.
X * 	IF YOU SCREW UP YOUR DISK WILL NEED TO BE REFORMATTED
X *
X * I used this to rename partition 3 MIX to prevent gemboot from
X * getting upset with a minix file system in it.
X */
X
X#include <stdio.h>
Xlong inchar = 0;
Xlong totchar = 0;
Xmain(argc,argv)
Xchar** argv;
X{
X	char *index();
X	long address,naddress;
X	unsigned short val;
X	FILE *outf;
X	char line[120];	
X	char *cp;
X	int j;
X	int len,length;
X	FILE *fdopen();
X
X	if(argc > 2)
X		usage(argv[0]);
X	if(argc == 1){ 
X		j = dup(fileno(stdout));
X		outf = fdopen(j,"w");
X	} else
X		outf = fopen ( argv[1],"w");
X	if(!outf) {
X		perror("open");
X		fprintf(stderr,"Cannot open %s\n");
X		exit(1);
X	}
X	address = 0;
X	for(;;) {
X		cp = fgets(line,sizeof(line),stdin);
X
X		if(!cp ) { 
Xbyebye:
X			fprintf(stderr,"EOF seen totchar out = %ld, in = %ld, done!\n",totchar,inchar);
Xbye:
X			fclose(outf);
X			fflush(stderr);
X			exit(1);
X		}
X		if(line[0] == '*') { /* fill will last val */
X/* get next line for it address */
X			cp = fgets(line,sizeof(line),stdin);
X			naddress = strtol(line,&cp,8);
X			if(!cp ) { 
X				fprintf(stderr,"Premature EOF do a * line\n");
X				goto bye;
X			}
X
X			while(address < naddress) {
X				for(j=0;j<8;j++) {
X					putw(val,outf);
X					address += sizeof(short);
X				}
X			}
X		/* just fall thru, as the line is already in the buffer */
X		}
X		length = strlen(line);
X		if(length <7) {
X			fprintf(stderr,"Invalid line\n");
X			goto bye;
X		}
X		inchar += length;
X		cp = index(line,' ');
X		if(!cp)
X			goto byebye;
X		len = cp - line +1;
X		len = (length - len)/5;
X
X		if(length  > 49  || len < 1){
X			fprintf(stderr,"Bad line: <%s> length=%d\n",line,strlen(line));
X			exit(1);
X		}
X		address = strtol(line,&cp,8);
X		for(j=0;j<len;j++) {
X			cp++;
X			val = strtol(cp,&cp,16);
X			putw(val,outf);
X			address += sizeof(short);
X		}
X	}
X	
X}
Xusage(s)
Xchar * s;
X{
X	fprintf(stderr,"Usage: %s %outfile < instream\n",s);
X	exit(1);
X}
X	
Xputw(w,fp)
XFILE* fp;
X{
X	putc(w>>8,fp);
X	putc(w,fp);
X	totchar += 2;
X}
X
/
echo x - tofile.doc
gres '^X' '' > tofile.doc << '/'
XTofile.c was written to change partition information in the boot
Xblock of the hard disk.  Actually, you can patch ANY file with it
Xas it takes the output of od -x and turns it back into a binary file.
X
XAs changing the boot block is DANGEROUS, i suggest the following:
X1. cat /dev/rhd0 > save  /* if save is on a floppy, all the better */
X2. od -x /dev/rhd0 > boot
X3. ed boot for the desired change in partitioning
X4. tofile /dev/rhd0 < boot
X5. od -x /dev/rhd0 >new_boot
X6. diff boot new_boot  /* Only your changes should be evident */
X
X7. REMEMBER, if you boot from the hard disk, the boot checksum is now
X   wrong.  You will have to boot TOS from floppy, and reinstall the boot program (or fix
X   the checksum).
X
X8. IF you have damaged the boot block on the hard disk you can restore
X   it from 'save' as cat save > /dev/rhd0.
X
XThe following is included to show what you may see.  Remember that if you
Xdo not boot from the hard disk it will contain repeated lines of 0 which
Xod will print only a portion.
X
Xod -x /dev/rhd0
X=====================THIS is the BOOT BLOCK=============
X0000000 6026 0200 0002 0400 001a 0041 0042 009d
X0000020 3fb0 0001 0000 0001 433a 5c41 4844 492e
X0000040 5052 4700 4861 686e 2a5f 2a5f 2c4f 41fa
X0000060 0184 8f10 3f3c ffff 3f3c 000b 4e4d 2e4e
X0000100 0240 000e 0c40 000e 677a 41f9 ffff 8800
X0000120 10bc 000e 117c 0025 0002 2838 0472 2a38
X0000140 0476 2c38 047e 43fa 0140 21c9 0472 43fa
X0000160 008a 21c9 0476 43fa 0138 21c9 047e 43fa
X0000200 ff98 4283 1611 963c 0041 2038 04c2 07c0
X0000220 21c0 04c2 4878 04be 4878 04be 2f09 3f3c
X0000240 0003 3f3c 004b 4e41 2e4e 21c4 0472 21c5
X0000260 0476 21c6 047e 2238 04c2 0781 21c1 04c2
X0000300 4a80 6c0e 4278 0446 4267 3f3c 000e 4e41
X0000320 6024 2640 42a7 2f0b 42a7 3f3c 0004 3f3c
X0000340 004b 4e41 4a80 6702 4283 206b 0024 1143
X0000360 0037 31c3 0446 2e4e 4ed5 4df9 ffff 8606
X0000400 4bf9 ffff 8604 49fa 00ac 4280 302f 000c
X0000420 d0ba ff02 3940 0002 4840 1940 0001 196f
X0000440 000b 0004 50f8 043e 13ef 0009 ffff 860d
X0000460 13ef 0008 ffff 860b 13ef 0007 ffff 8609
X0000500 3cbc 0198 3cbc 0098 3aaf 000a 49fa 0066
X0000520 3cbc 0088 7c04 203c 008a 0000 101c 4840
X0000540 2a80 720a 612c 51ce ffee 2abc 0000 000a
X0000560 323c 0258 611c 4280 3cbc 008a 3015 c07c
X0000600 00ff 6702 70ff 3cbc 0080 4278 043e 4a80
X0000620 4e75 d2b8 04ba 0839 0005 ffff fa01 67f0
X0000640 b2b8 04ba 66f0 60dc 41fa fe58 2008 4e75
X0000660 4280 4e75 0800 0395 0903 0265 0265 0402
X0000700 0111 0002 240d 0147 454d 0000 0001 0000
X0000720 7ffe 0147 454d 0000 7fff 0000 6ffe 014d
X0000740 494e 0000 effd 0000 67fe 0147 454d 0001
X0000760 57fb 0000 67fe 0000 0000 0000 0000 1537
X0001000
X================END OF THE BOOT =================
X
X===========This is informational only===========
Xthis is part of od -c /dev/rhd0
XNotice 4 partitions; GEM, GEM, MIX, GEM
X0000700 001 021  \0 002   $  \r 001   G   E   M  \0  \0  \0 001  \0  \0
X0000720 177 376 001   G   E   M  \0  \0 177 377  \0  \0   o 376 001   M
X0000740   I   X  \0  \0 357 375  \0  \0   g 376 001   G   E   M  \0 001
X0000760   W 373  \0  \0   g 376  \0  \0  \0  \0  \0  \0  \0  \0 025   7
X0001000
/