File: /usr/src/linux/arch/ppc/boot/utils/mkprep.c

1     /*
2      * BK Id: SCCS/s.mkprep.c 1.7 05/18/01 06:20:29 patch
3      */
4     /*
5      * Makes a prep bootable image which can be dd'd onto
6      * a disk device to make a bootdisk.  Will take
7      * as input a elf executable, strip off the header
8      * and write out a boot image as:
9      * 1) default - strips elf header
10      *      suitable as a network boot image
11      * 2) -pbp - strips elf header and writes out prep boot partition image
12      *      cat or dd onto disk for booting
13      * 3) -asm - strips elf header and writes out as asm data
14      *      useful for generating data for a compressed image
15      *                  -- Cort
16      *
17      * Modified for x86 hosted builds by Matt Porter <porter@neta.com>
18      */
19     
20     #include <fcntl.h>
21     #include <stdio.h>
22     #include <stdlib.h>
23     #include <string.h>
24     #include <sys/stat.h>
25     #include <unistd.h>
26     
27     #define cpu_to_le32(x) le32_to_cpu((x))
28     unsigned long le32_to_cpu(unsigned long x)
29     {
30          	return (((x & 0x000000ffU) << 24) |
31     		((x & 0x0000ff00U) <<  8) |
32     		((x & 0x00ff0000U) >>  8) |
33     		((x & 0xff000000U) >> 24));
34     }
35     
36     
37     #define cpu_to_le16(x) le16_to_cpu((x))
38     unsigned short le16_to_cpu(unsigned short x)
39     {
40     	return (((x & 0x00ff) << 8) |
41     		((x & 0xff00) >> 8));
42     }
43     
44     #define cpu_to_be32(x) (x)
45     #define be32_to_cpu(x) (x)
46     #define cpu_to_be16(x) (x)
47     #define be16_to_cpu(x) (x)
48     
49     /* size of read buffer */
50     #define SIZE 0x1000
51     
52     
53     typedef unsigned long dword_t;
54     typedef unsigned short word_t;
55     typedef unsigned char byte_t;
56     typedef byte_t block_t[512];
57     typedef byte_t page_t[4096];
58     
59     
60     /*
61      * Partition table entry
62      *  - from the PReP spec
63      */
64     typedef struct partition_entry {
65       byte_t	boot_indicator;
66       byte_t	starting_head;
67       byte_t	starting_sector;
68       byte_t	starting_cylinder;
69     
70       byte_t	system_indicator;
71       byte_t	ending_head;
72       byte_t	ending_sector;
73       byte_t	ending_cylinder;
74     
75       dword_t	beginning_sector;
76       dword_t	number_of_sectors;
77     } partition_entry_t;
78     
79     #define BootActive	0x80
80     #define SystemPrep	0x41
81     
82     void copy_image(int , int);
83     void write_prep_partition(int , int );
84     void write_asm_data( int in, int out );
85     
86     unsigned int elfhdr_size = 65536;
87     
88     int main(int argc, char *argv[])
89     {
90       int in_fd, out_fd;
91       int argptr = 1;
92       unsigned int prep = 0;
93       unsigned int asmoutput = 0;
94     
95       if ( (argc < 3) || (argc > 4) )
96       {
97         fprintf(stderr, "usage: %s [-pbp] [-asm] <boot-file> <image>\n",argv[0]);
98         exit(-1);
99       }
100     
101       /* needs to handle args more elegantly -- but this is a small/simple program */
102       
103       /* check for -pbp */
104       if ( !strcmp( argv[argptr], "-pbp" ) )
105       {
106         prep = 1;
107         argptr++;
108       }
109       
110       /* check for -asm */
111       if ( !strcmp( argv[argptr], "-asm" ) )
112       {
113         asmoutput = 1;
114         argptr++;
115       }
116     
117       /* input file */
118       if ( !strcmp( argv[argptr], "-" ) )
119         in_fd = 0;			/* stdin */
120       else
121         if ((in_fd = open( argv[argptr] , 0)) < 0)
122           exit(-1);
123       argptr++;
124     
125       /* output file */
126       if ( !strcmp( argv[argptr], "-" ) )
127         out_fd = 1;			/* stdout */
128       else
129         if ((out_fd = creat( argv[argptr] , 0755)) < 0)
130           exit(-1);
131       argptr++;
132     
133       /* skip elf header in input file */
134       /*if ( !prep )*/
135       lseek(in_fd, elfhdr_size, SEEK_SET);
136       
137       /* write prep partition if necessary */
138       if ( prep )
139     	  write_prep_partition( in_fd, out_fd );
140     
141       /* write input image to bootimage */
142       if ( asmoutput )
143     	  write_asm_data( in_fd, out_fd );
144       else
145     	  copy_image(in_fd, out_fd);
146       
147       return 0;
148     }
149     
150     void write_prep_partition(int in, int out)
151     {
152       unsigned char block[512];
153       partition_entry_t *pe = (partition_entry_t *)&block[0x1BE];
154       dword_t *entry  = (dword_t *)&block[0];
155       dword_t *length = (dword_t *)&block[sizeof(long)];
156       struct stat info;
157       
158       if (fstat(in, &info) < 0)
159       {
160         fprintf(stderr,"info failed\n");
161         exit(-1);
162       }
163       
164       bzero( block, sizeof block );
165      
166       /* set entry point and boot image size skipping over elf header */
167     #ifdef __i386__
168       *entry = 0x400/*+65536*/;
169       *length = info.st_size-elfhdr_size+0x400;
170     #else
171       *entry = cpu_to_le32(0x400/*+65536*/);
172       *length = cpu_to_le32(info.st_size-elfhdr_size+0x400);
173     #endif /* __i386__ */
174     
175       /* sets magic number for msdos partition (used by linux) */
176       block[510] = 0x55;
177       block[511] = 0xAA;
178       
179       /*
180        * Build a "PReP" partition table entry in the boot record
181        *  - "PReP" may only look at the system_indicator
182        */
183       pe->boot_indicator   = BootActive;
184       pe->system_indicator = SystemPrep;
185       /*
186        * The first block of the diskette is used by this "boot record" which
187        * actually contains the partition table. (The first block of the
188        * partition contains the boot image, but I digress...)  We'll set up
189        * one partition on the diskette and it shall contain the rest of the
190        * diskette.
191        */
192       pe->starting_head     = 0;	/* zero-based			     */
193       pe->starting_sector   = 2;	/* one-based			     */
194       pe->starting_cylinder = 0;	/* zero-based			     */
195       pe->ending_head       = 1;	/* assumes two heads		     */
196       pe->ending_sector     = 18;	/* assumes 18 sectors/track	     */
197       pe->ending_cylinder   = 79;	/* assumes 80 cylinders/diskette     */
198     
199       /*
200        * The "PReP" software ignores the above fields and just looks at
201        * the next two.
202        *   - size of the diskette is (assumed to be)
203        *     (2 tracks/cylinder)(18 sectors/tracks)(80 cylinders/diskette)
204        *   - unlike the above sector numbers, the beginning sector is zero-based!
205        */
206     #if 0
207       pe->beginning_sector  = cpu_to_le32(1);
208     #else
209       /* This has to be 0 on the PowerStack? */   
210     #ifdef __i386__
211       pe->beginning_sector  = 0;
212     #else
213       pe->beginning_sector  = cpu_to_le32(0);
214     #endif /* __i386__ */
215     #endif    
216     
217     #ifdef __i386__
218       pe->number_of_sectors = 2*18*80-1;
219     #else
220       pe->number_of_sectors = cpu_to_le32(2*18*80-1);
221     #endif /* __i386__ */
222     
223       write( out, block, sizeof(block) );
224       write( out, entry, sizeof(*entry) );
225       write( out, length, sizeof(*length) );  
226       /* set file position to 2nd sector where image will be written */
227       lseek( out, 0x400, SEEK_SET );
228     }
229     
230     
231     
232     void
233     copy_image(int in, int out)
234     {
235       char buf[SIZE];
236       int n;
237     
238       while ( (n = read(in, buf, SIZE)) > 0 )
239         write(out, buf, n);
240     }
241     
242     
243     void
244     write_asm_data( int in, int out )
245     {
246       int i, cnt, pos, len;
247       unsigned int cksum, val;
248       unsigned char *lp;
249       unsigned char buf[SIZE];
250       unsigned char str[256];
251       
252       write( out, "\t.data\n\t.globl input_data\ninput_data:\n",
253     	 strlen( "\t.data\n\t.globl input_data\ninput_data:\n" ) );
254       pos = 0;
255       cksum = 0;
256       while ((len = read(in, buf, sizeof(buf))) > 0)
257       {
258         cnt = 0;
259         lp = (unsigned char *)buf;
260         len = (len + 3) & ~3;  /* Round up to longwords */
261         for (i = 0;  i < len;  i += 4)
262         {
263           if (cnt == 0)
264           {
265     	write( out, "\t.long\t", strlen( "\t.long\t" ) );
266           }
267           sprintf( str, "0x%02X%02X%02X%02X", lp[0], lp[1], lp[2], lp[3]);
268           write( out, str, strlen(str) );
269           val = *(unsigned long *)lp;
270           cksum ^= val;
271           lp += 4;
272           if (++cnt == 4)
273           {
274     	cnt = 0;
275     	sprintf( str, " # %x \n", pos+i-12);
276     	write( out, str, strlen(str) );
277           } else
278           {
279     	write( out, ",", 1 );
280           }
281         }
282         if (cnt)
283         {
284           write( out, "0\n", 2 );
285         }
286         pos += len;
287       }
288       sprintf(str, "\t.globl input_len\ninput_len:\t.long\t0x%x\n", pos);
289       write( out, str, strlen(str) );
290     
291       fprintf(stderr, "cksum = %x\n", cksum);
292     }
293