boot.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <libsec.h>
  5. #include "iso9660.h"
  6. /* FreeBSD 4.5 installation CD for reference
  7. g% cdsector 17 | xd -b
  8. 1+0 records in
  9. 1+0 records out
  10. 0000000 00 - volume descriptor type (0)
  11. 43 44 30 30 31 - "CD001"
  12. 01 - volume descriptor version (1)
  13. 45 4c 20 54 4f 52 49 54 4f
  14. 0000010 20 53 50 45 43 49 46 49 43 41 54 49 4f 4e 00 00
  15. 0000020 00 00 00 00 00 00 00 - 7-39 boot system identifier
  16. "EL TORITO SPECIFICATION"
  17. 00 00 00 00 00 00 00 00 00
  18. 0000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  19. 0000040 00 00 00 00 00 00 00 - 39-71 boot identifier
  20. boot system use:
  21. absolute pointer to the boot catalog??
  22. 4d 0c 00 00 00 00 00 00 00
  23. 0000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  24. 0000580 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  25. 0000590 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  26. 00005a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  27. g% cdsector 3149 | xd -b # 0x0c4d
  28. 1+0 records in
  29. 1+0 records out
  30. 0000000 01 - header (1)
  31. 00 - platform id (0 = 0x86)
  32. 00 00 - reserved 0
  33. 00 00 00 00 00 00 00 00 00 00 00 00
  34. 0000010 00 00 00 00 00 00 00 00 00 00 00 00 - id string
  35. aa 55 - checksum
  36. 55 aa - magic
  37. 0000020 88 - 88 = bootable
  38. 03 - 3 = 2.88MB diskette
  39. 00 00 - load segment 0 means default 0x7C0
  40. 00 - system type (byte 5 of boot image)
  41. 00 - unused (0)
  42. 01 00 - 512-byte sector count for initial load
  43. 4e 0c 00 00 - ptr to virtual disk
  44. 00 00 00 00
  45. 0000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  46. 0000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  47. 0000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  48. 0000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  49. g% cdsector `{h2d 0c4e} | xd -b
  50. 1+0 records in
  51. 1+0 records out
  52. 0000000 eb 3c 00 00 00 00 00 00 00 00 00 00 02 00 00 00
  53. 0000010 00 00 00 00 00 00 00 00 12 00 02 00 00 00 00 00
  54. 0000020 00 00 00 00 00 16 1f 66 6a 00 51 50 06 53
  55. 31 c0
  56. FREEBSD
  57. 0000000 eb 3c 00 00 00 00 00 00 00 00 00 00 02 00 00 00
  58. 0000010 00 00 00 00 00 00 00 00 12 00 02 00 00 00 00 00
  59. 0000020 00 00 00 00 00 16 1f 66 6a 00 51 50 06 53
  60. 31 c0
  61. DOS 5
  62. 0000000 eb 3c 90 4d 53 44 4f 53 35 2e 30 00 02 01 01 00
  63. 0000010 02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00
  64. 0000020 00 00 00 00 00 00 29 6a 2c e0 16 4e 4f 20 4e 41
  65. 0000030 4d 45 20 20 20 20 46 41 54 31 32 20 20 20 fa 33
  66. 0000040 c0 8e d0 bc 00 7c 16 07 bb 78 00 36 c5 37 1e 56
  67. 0000050 16 53 bf 3e 7c b9 0b 00 fc f3 a4 06 1f c6 45 fe
  68. 0000060 0f 8b 0e 18 7c 88 4d f9 89 47 02 c7 07 3e 7c fb
  69. 0000070 cd 13 72 79 33 c0 39 06 13 7c 74 08 8b 0e 13 7c
  70. 0000080 89 0e 20 7c a0 10 7c f7 26 16 7c 03 06 1c 7c 13
  71. 0000090 16 1e 7c 03 06 0e 7c 83 d2 00 a3 50 7c 89 16 52
  72. 00000a0 7c a3 49 7c 89 16 4b 7c b8 20 00 f7 26 11 7c 8b
  73. NDISK
  74. 0000000 eb 3c 90 50 6c 61 6e 39 2e 30 30 00 02 01 01 00
  75. 0000010 02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00
  76. 0000020 40 0b 00 00 00 00 29 13 00 00 00 43 59 4c 49 4e
  77. 0000030 44 52 49 43 41 4c 46 41 54 31 32 20 20 20 fa 31
  78. 0000040 c0 8e d0 8e d8 8e c0 bc ec 7b 89 e5 88 56 12 fb
  79. 0000050 be ea 7d bf 90 7d ff d7 bf 82 7d ff d7 8b 06 27
  80. 0000060 7c 8b 16 29 7c bb 00 7e bf 2c 7d ff d7 bb 10 00
  81. PBSDISK
  82. 0000000 eb 3c 90 50 6c 61 6e 39 2e 30 30 00 02 01 01 00
  83. 0000010 02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00
  84. 0000020 40 0b 00 00 00 00 29 13 00 00 00 43 59 4c 49 4e
  85. 0000030 44 52 49 43 41 4c 46 41 54 31 32 20 20 20 fa 31
  86. 0000040 c0 8e d0 8e d8 8e c0 bc f8 7b 89 e5 88 56 00 fb
  87. 0000050 be f8 7d bf 00 7d ff d7 bf df 7c ff d7 8b 06 27
  88. 0000060 7c 8b 16 29 7c bb 00 7e bf 89 7c ff d7 bf fb 7c
  89. */
  90. void
  91. Cputbootvol(Cdimg *cd)
  92. {
  93. Cputc(cd, 0x00);
  94. Cputs(cd, "CD001", 5);
  95. Cputc(cd, 0x01);
  96. Cputs(cd, "EL TORITO SPECIFICATION", 2+1+6+1+13);
  97. Crepeat(cd, 0, 2+16+16+7);
  98. cd->bootcatptr = Cwoffset(cd);
  99. Cpadblock(cd);
  100. }
  101. void
  102. Cupdatebootvol(Cdimg *cd)
  103. {
  104. uvlong o;
  105. o = Cwoffset(cd);
  106. Cwseek(cd, cd->bootcatptr);
  107. Cputnl(cd, cd->bootcatblock, 4);
  108. Cwseek(cd, o);
  109. }
  110. void
  111. Cputbootcat(Cdimg *cd)
  112. {
  113. cd->bootcatblock = Cwoffset(cd) / Blocksize;
  114. Cputc(cd, 0x01);
  115. Cputc(cd, 0x00);
  116. Cputc(cd, 0x00);
  117. Cputc(cd, 0x00);
  118. Crepeat(cd, 0, 12+12);
  119. /*
  120. * either the checksum doesn't include the header word
  121. * or it just doesn't matter.
  122. */
  123. Cputc(cd, 0xAA);
  124. Cputc(cd, 0x55);
  125. Cputc(cd, 0x55);
  126. Cputc(cd, 0xAA);
  127. cd->bootimageptr = Cwoffset(cd);
  128. Cpadblock(cd);
  129. }
  130. enum {
  131. Emusectsz = 512, /* bytes per emulated sector */
  132. };
  133. void
  134. Cupdatebootcat(Cdimg *cd)
  135. {
  136. uvlong o;
  137. int n;
  138. if(cd->bootdirec == nil)
  139. return;
  140. o = Cwoffset(cd);
  141. Cwseek(cd, cd->bootimageptr);
  142. Cputc(cd, 0x88);
  143. if(cd->flags & CDbootnoemu)
  144. Cputc(cd, 0); /* no disk emulation */
  145. else
  146. switch(cd->bootdirec->length){
  147. default:
  148. fprint(2, "warning: boot image is not 1.44MB or 2.88MB; "
  149. "pretending 1.44MB\n");
  150. /* fall through */
  151. case 1440*1024:
  152. Cputc(cd, 0x02); /* 1.44MB disk */
  153. break;
  154. case 2880*1024:
  155. Cputc(cd, 0x03); /* 2.88MB disk */
  156. break;
  157. }
  158. Cputnl(cd, 0, 2); /* load segment */
  159. Cputc(cd, 0); /* system type */
  160. Cputc(cd, 0); /* unused */
  161. n = 1;
  162. if(cd->flags & CDbootnoemu){
  163. n = (cd->bootdirec->length + Emusectsz - 1) / Emusectsz;
  164. if(n > 4){
  165. fprint(2, "warning: boot image too big; "
  166. "will only load the first 2K\n");
  167. n = 4;
  168. }
  169. }
  170. Cputnl(cd, n, 2); /* Emusectsz-byte sector count for load */
  171. Cputnl(cd, cd->bootdirec->block, 4); /* ptr to disk image */
  172. Cwseek(cd, o);
  173. }
  174. void
  175. findbootimage(Cdimg *cd, Direc *root)
  176. {
  177. Direc *d;
  178. d = walkdirec(root, cd->bootimage);
  179. if(d == nil){
  180. fprint(2, "warning: did not encounter boot image\n");
  181. return;
  182. }
  183. cd->bootdirec = d;
  184. }