bootld.c 1.8 KB

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