boot.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "/sys/src/libmach/elf.h"
  16. static uint8_t elfident[7] = {
  17. '\177', 'E', 'L', 'F', '\1', '\1', '\1'
  18. };
  19. static Ehdr ehdr, rehdr;
  20. static Phdr *phdr;
  21. static int curphdr;
  22. static uint32_t curoff;
  23. static uint32_t elftotal;
  24. static int32_t (*swal)(int32_t);
  25. static uint16_t (*swab)(uint16_t);
  26. /*
  27. * big-endian short
  28. */
  29. uint16_t
  30. beswab(uint16_t s)
  31. {
  32. uint8_t *p;
  33. p = (uint8_t*)&s;
  34. return (p[0]<<8) | p[1];
  35. }
  36. /*
  37. * big-endian long
  38. */
  39. int32_t
  40. beswal(int32_t l)
  41. {
  42. uint8_t *p;
  43. p = (uint8_t*)&l;
  44. return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  45. }
  46. /*
  47. * big-endian vlong
  48. */
  49. uint64_t
  50. beswav(uint64_t v)
  51. {
  52. uint8_t *p;
  53. p = (uint8_t*)&v;
  54. return ((uint64_t)p[0]<<56) | ((uint64_t)p[1]<<48) | ((uint64_t)p[2]<<40)
  55. | ((uint64_t)p[3]<<32) | ((uint64_t)p[4]<<24)
  56. | ((uint64_t)p[5]<<16) | ((uint64_t)p[6]<<8)
  57. | (uint64_t)p[7];
  58. }
  59. /*
  60. * little-endian short
  61. */
  62. uint16_t
  63. leswab(uint16_t s)
  64. {
  65. uint8_t *p;
  66. p = (uint8_t*)&s;
  67. return (p[1]<<8) | p[0];
  68. }
  69. /*
  70. * little-endian long
  71. */
  72. int32_t
  73. leswal(int32_t l)
  74. {
  75. uint8_t *p;
  76. p = (uint8_t*)&l;
  77. return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  78. }
  79. /*
  80. * Convert header to canonical form
  81. */
  82. static void
  83. hswal(int32_t *lp, int n, int32_t (*swap) (int32_t))
  84. {
  85. while (n--) {
  86. *lp = (*swap) (*lp);
  87. lp++;
  88. }
  89. }
  90. static int
  91. readehdr(Boot *b)
  92. {
  93. int i;
  94. /* bitswap the header according to the DATA format */
  95. if(ehdr.ident[CLASS] != ELFCLASS32) {
  96. print("bad ELF class - not 32 bit\n");
  97. return 0;
  98. }
  99. if(ehdr.ident[DATA] == ELFDATA2LSB) {
  100. swab = leswab;
  101. swal = leswal;
  102. } else if(ehdr.ident[DATA] == ELFDATA2MSB) {
  103. swab = beswab;
  104. swal = beswal;
  105. } else {
  106. print("bad ELF encoding - not big or little endian\n");
  107. return 0;
  108. }
  109. memmove(&rehdr, &ehdr, sizeof(Ehdr));
  110. ehdr.type = swab(ehdr.type);
  111. ehdr.machine = swab(ehdr.machine);
  112. ehdr.version = swal(ehdr.version);
  113. ehdr.elfentry = swal(ehdr.elfentry);
  114. ehdr.phoff = swal(ehdr.phoff);
  115. ehdr.shoff = swal(ehdr.shoff);
  116. ehdr.flags = swal(ehdr.flags);
  117. ehdr.ehsize = swab(ehdr.ehsize);
  118. ehdr.phentsize = swab(ehdr.phentsize);
  119. ehdr.phnum = swab(ehdr.phnum);
  120. ehdr.shentsize = swab(ehdr.shentsize);
  121. ehdr.shnum = swab(ehdr.shnum);
  122. ehdr.shstrndx = swab(ehdr.shstrndx);
  123. if(ehdr.type != EXEC || ehdr.version != CURRENT)
  124. return 0;
  125. if(ehdr.phentsize != sizeof(Phdr))
  126. return 0;
  127. if(debug)
  128. print("readehdr OK entry 0x%lux\n", ehdr.elfentry);
  129. curoff = sizeof(Ehdr);
  130. i = ehdr.phoff+ehdr.phentsize*ehdr.phnum - curoff;
  131. b->state = READPHDR;
  132. b->bp = (char*)malloc(i);
  133. b->wp = b->bp;
  134. b->ep = b->wp + i;
  135. phdr = (Phdr*)(b->bp + ehdr.phoff-sizeof(Ehdr));
  136. if(debug)
  137. print("phdr...");
  138. return 1;
  139. }
  140. static int
  141. nextphdr(Boot *b)
  142. {
  143. Phdr *php;
  144. uint32_t entry, offset;
  145. char *paddr;
  146. if(debug)
  147. print("readedata %d\n", curphdr);
  148. for(; curphdr < ehdr.phnum; curphdr++){
  149. php = phdr+curphdr;
  150. if(php->type != LOAD)
  151. continue;
  152. offset = php->offset;
  153. paddr = (char*)PADDR(php->paddr);
  154. if(offset < curoff){
  155. /*
  156. * Can't (be bothered to) rewind the
  157. * input, it might be from tftp. If we
  158. * did then we could boot FreeBSD kernels
  159. * too maybe.
  160. */
  161. return 0;
  162. }
  163. if(php->offset > curoff){
  164. b->state = READEPAD;
  165. b->bp = (char*)malloc(offset - curoff);
  166. b->wp = b->bp;
  167. b->ep = b->wp + offset - curoff;
  168. if(debug)
  169. print("nextphdr %lud...\n", offset - curoff);
  170. return 1;
  171. }
  172. b->state = READEDATA;
  173. b->bp = paddr;
  174. b->wp = b->bp;
  175. b->ep = b->wp+php->filesz;
  176. print("%ud+", php->filesz);
  177. elftotal += php->filesz;
  178. if(debug)
  179. print("nextphdr %ud@0x%p\n", php->filesz, paddr);
  180. return 1;
  181. }
  182. if(curphdr != 0){
  183. print("=%lud\n", elftotal);
  184. b->state = TRYEBOOT;
  185. entry = ehdr.elfentry & ~0xF0000000;
  186. PLLONG(b->hdr.entry, entry);
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static int
  192. readepad(Boot *b)
  193. {
  194. Phdr *php;
  195. php = phdr+curphdr;
  196. if(debug)
  197. print("readepad %d\n", curphdr);
  198. curoff = php->offset;
  199. return nextphdr(b);
  200. }
  201. static int
  202. readedata(Boot *b)
  203. {
  204. Phdr *php;
  205. php = phdr+curphdr;
  206. if(debug)
  207. print("readedata %d\n", curphdr);
  208. if(php->filesz < php->memsz){
  209. print("%lud", php->memsz-php->filesz);
  210. elftotal += php->memsz-php->filesz;
  211. memset((char*)(PADDR(php->paddr)+php->filesz), 0,
  212. php->memsz-php->filesz);
  213. }
  214. curoff = php->offset+php->filesz;
  215. curphdr++;
  216. return nextphdr(b);
  217. }
  218. static int
  219. readphdr(Boot *b)
  220. {
  221. Phdr *php;
  222. php = phdr;
  223. hswal((int32_t*)php, ehdr.phentsize*ehdr.phnum/sizeof(int32_t), swal);
  224. if(debug)
  225. print("phdr curoff %lud vaddr 0x%lux paddr 0x%lux\n",
  226. curoff, php->vaddr, php->paddr);
  227. curoff = ehdr.phoff+ehdr.phentsize*ehdr.phnum;
  228. curphdr = 0;
  229. return nextphdr(b);
  230. }
  231. static int
  232. addbytes(char **dbuf, char *edbuf, char **sbuf, char *esbuf)
  233. {
  234. int n;
  235. n = edbuf - *dbuf;
  236. if(n <= 0)
  237. return 0;
  238. if(n > esbuf - *sbuf)
  239. n = esbuf - *sbuf;
  240. if(n <= 0)
  241. return -1;
  242. memmove(*dbuf, *sbuf, n);
  243. *sbuf += n;
  244. *dbuf += n;
  245. return edbuf - *dbuf;
  246. }
  247. int
  248. bootpass(Boot *b, void *vbuf, int nbuf)
  249. {
  250. char *buf, *ebuf;
  251. Hdr *hdr;
  252. uint32_t magic, entry, data, text, bss;
  253. uint64_t entry64;
  254. if(b->state == FAILED)
  255. return FAIL;
  256. if(nbuf == 0)
  257. goto Endofinput;
  258. buf = vbuf;
  259. ebuf = buf+nbuf;
  260. while(addbytes(&b->wp, b->ep, &buf, ebuf) == 0) {
  261. switch(b->state) {
  262. case INITKERNEL:
  263. b->state = READEXEC;
  264. b->bp = (char*)&b->hdr;
  265. b->wp = b->bp;
  266. b->ep = b->bp+sizeof(Hdr);
  267. break;
  268. case READEXEC:
  269. hdr = &b->hdr;
  270. magic = GLLONG(hdr->magic);
  271. if(magic == I_MAGIC || magic == S_MAGIC) {
  272. b->state = READ9TEXT;
  273. b->bp = (char*)PADDR(GLLONG(hdr->entry));
  274. b->wp = b->bp;
  275. b->ep = b->wp+GLLONG(hdr->text);
  276. if(magic == I_MAGIC){
  277. memmove(b->bp, b->hdr.uvl, sizeof(b->hdr.uvl));
  278. b->wp += sizeof(b->hdr.uvl);
  279. }
  280. print("%lud", GLLONG(hdr->text));
  281. break;
  282. }
  283. /* check for gzipped kernel */
  284. if(b->bp[0] == 0x1F && (uint8_t)b->bp[1] == 0x8B && b->bp[2] == 0x08) {
  285. b->state = READGZIP;
  286. b->bp = (char*)malloc(1440*1024);
  287. b->wp = b->bp;
  288. b->ep = b->wp + 1440*1024;
  289. memmove(b->bp, &b->hdr, sizeof(Hdr));
  290. b->wp += sizeof(Hdr);
  291. print("gz...");
  292. break;
  293. }
  294. /*
  295. * Check for ELF.
  296. */
  297. if(memcmp(b->bp, elfident, 4) == 0){
  298. b->state = READEHDR;
  299. b->bp = (char*)&ehdr;
  300. b->wp = b->bp;
  301. b->ep = b->wp + sizeof(Ehdr);
  302. memmove(b->bp, &b->hdr, sizeof(Hdr));
  303. b->wp += sizeof(Hdr);
  304. print("elf...");
  305. break;
  306. }
  307. print("bad kernel format (magic == %#lux)\n", magic);
  308. b->state = FAILED;
  309. return FAIL;
  310. case READ9TEXT:
  311. hdr = &b->hdr;
  312. b->state = READ9DATA;
  313. b->bp = (char*)PGROUND(PADDR(GLLONG(hdr->entry))+GLLONG(hdr->text));
  314. b->wp = b->bp;
  315. b->ep = b->wp + GLLONG(hdr->data);
  316. print("+%ld", GLLONG(hdr->data));
  317. break;
  318. case READ9DATA:
  319. hdr = &b->hdr;
  320. bss = GLLONG(hdr->bss);
  321. memset(b->ep, 0, bss);
  322. print("+%ld=%ld\n",
  323. bss, GLLONG(hdr->text)+GLLONG(hdr->data)+bss);
  324. b->state = TRYBOOT;
  325. return ENOUGH;
  326. case READEHDR:
  327. if(!readehdr(b)){
  328. print("readehdr failed\n");
  329. b->state = FAILED;
  330. return FAIL;
  331. }
  332. break;
  333. case READPHDR:
  334. if(!readphdr(b)){
  335. b->state = FAILED;
  336. return FAIL;
  337. }
  338. break;
  339. case READEPAD:
  340. if(!readepad(b)){
  341. b->state = FAILED;
  342. return FAIL;
  343. }
  344. break;
  345. case READEDATA:
  346. if(!readedata(b)){
  347. b->state = FAILED;
  348. return FAIL;
  349. }
  350. if(b->state == TRYBOOT)
  351. return ENOUGH;
  352. break;
  353. case TRYBOOT:
  354. case TRYEBOOT:
  355. case READGZIP:
  356. return ENOUGH;
  357. case READ9LOAD:
  358. case INIT9LOAD:
  359. panic("9load");
  360. default:
  361. panic("bootstate");
  362. }
  363. }
  364. return MORE;
  365. Endofinput:
  366. /* end of input */
  367. switch(b->state) {
  368. case INITKERNEL:
  369. case READEXEC:
  370. case READ9TEXT:
  371. case READ9DATA:
  372. case READEHDR:
  373. case READPHDR:
  374. case READEPAD:
  375. case READEDATA:
  376. print("premature EOF\n");
  377. b->state = FAILED;
  378. return FAIL;
  379. case TRYBOOT:
  380. entry = GLLONG(b->hdr.entry);
  381. magic = GLLONG(b->hdr.magic);
  382. if(magic == I_MAGIC){
  383. print("entry: 0x%lux\n", entry);
  384. warp9(PADDR(entry));
  385. }
  386. else if(magic == S_MAGIC){
  387. entry64 = beswav(b->hdr.uvl[0]);
  388. warp64(entry64);
  389. }
  390. b->state = FAILED;
  391. return FAIL;
  392. case TRYEBOOT:
  393. entry = GLLONG(b->hdr.entry);
  394. if(ehdr.machine == I386){
  395. print("entry: 0x%lux\n", entry);
  396. warp9(PADDR(entry));
  397. }
  398. else if(ehdr.machine == AMD64){
  399. print("entry: 0x%lux\n", entry);
  400. warp64(entry);
  401. }
  402. b->state = FAILED;
  403. return FAIL;
  404. case READGZIP:
  405. hdr = &b->hdr;
  406. if(b->bp[0] != 0x1F || (uint8_t)b->bp[1] != 0x8B || b->bp[2] != 0x08)
  407. print("lost magic\n");
  408. print("%ld => ", b->wp - b->bp);
  409. if(gunzip((uint8_t*)hdr, sizeof(*hdr), (uint8_t*)b->bp, b->wp - b->bp) < sizeof(*hdr)) {
  410. print("badly compressed kernel\n");
  411. return FAIL;
  412. }
  413. entry = GLLONG(hdr->entry);
  414. text = GLLONG(hdr->text);
  415. data = GLLONG(hdr->data);
  416. bss = GLLONG(hdr->bss);
  417. print("%lud+%lud+%lud=%lud\n", text, data, bss, text+data+bss);
  418. if(gunzip((uint8_t*)PADDR(entry)-sizeof(Exec), sizeof(Exec)+text+data,
  419. (uint8_t*)b->bp, b->wp-b->bp) < sizeof(Exec)+text+data) {
  420. print("error uncompressing kernel\n");
  421. return FAIL;
  422. }
  423. /* relocate data to start at page boundary */
  424. memmove((void*)PGROUND(PADDR(entry+text)), (void*)(PADDR(entry+text)), data);
  425. entry = GLLONG(b->hdr.entry);
  426. magic = GLLONG(b->hdr.magic);
  427. if(magic == I_MAGIC){
  428. print("entry: 0x%lux\n", entry);
  429. warp9(PADDR(entry));
  430. }
  431. else if(magic == S_MAGIC){
  432. entry64 = beswav(b->hdr.uvl[0]);
  433. warp64(entry64);
  434. }
  435. b->state = FAILED;
  436. return FAIL;
  437. case INIT9LOAD:
  438. case READ9LOAD:
  439. panic("end 9load");
  440. default:
  441. panic("bootdone");
  442. }
  443. b->state = FAILED;
  444. return FAIL;
  445. }