mkprep.c 6.9 KB

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