bootld.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. static int
  8. addbytes(char **dbuf, char *edbuf, char **sbuf, char *esbuf)
  9. {
  10. int n;
  11. n = edbuf - *dbuf;
  12. if(n <= 0)
  13. return 0;
  14. if(n > esbuf - *sbuf)
  15. n = esbuf - *sbuf;
  16. if(n <= 0)
  17. return -1;
  18. memmove(*dbuf, *sbuf, n);
  19. *sbuf += n;
  20. *dbuf += n;
  21. return edbuf - *dbuf;
  22. }
  23. extern void origin(void);
  24. int
  25. bootpass(Boot *b, void *vbuf, int nbuf)
  26. {
  27. char *buf, *ebuf, *p, *q;
  28. ulong size;
  29. if(b->state == FAILED)
  30. return FAIL;
  31. if(nbuf == 0)
  32. goto Endofinput;
  33. buf = vbuf;
  34. ebuf = buf+nbuf;
  35. while(addbytes(&b->wp, b->ep, &buf, ebuf) == 0) {
  36. switch(b->state) {
  37. case INIT9LOAD:
  38. b->state = READ9LOAD;
  39. b->bp = (char*)0x10000;
  40. b->wp = b->bp;
  41. b->ep = b->bp + 256*1024;
  42. break;
  43. case READ9LOAD:
  44. return ENOUGH;
  45. default:
  46. panic("bootstate");
  47. }
  48. }
  49. return MORE;
  50. Endofinput:
  51. /* end of input */
  52. print("\n");
  53. switch(b->state) {
  54. case INIT9LOAD:
  55. print("premature EOF\n");
  56. b->state = FAILED;
  57. return FAIL;
  58. case READ9LOAD:
  59. size = b->wp - b->bp;
  60. if(memcmp(b->bp, origin, 16) != 0) {
  61. print("beginning of file does not look right\n");
  62. b->state = FAILED;
  63. return FAIL;
  64. }
  65. if(size < 32*1024 || size > 256*1024) {
  66. print("got %lud byte loader; not likely\n", size);
  67. b->state = FAILED;
  68. return FAIL;
  69. }
  70. p = b->bp;
  71. q = b->wp;
  72. if(q - p > 10000) /* don't search much past l.s */
  73. q = p+10000;
  74. /*
  75. * The string `JUMP' appears right before
  76. * tokzero, which is where we want to jump.
  77. */
  78. for(; p<q; p++) {
  79. if(strncmp(p, "JUMP", 4) == 0) {
  80. p += 4;
  81. warp9((ulong)p);
  82. }
  83. }
  84. print("could not find jump destination\n");
  85. b->state = FAILED;
  86. return FAIL;
  87. default:
  88. panic("bootdone");
  89. }
  90. b->state = FAILED;
  91. return FAIL;
  92. }