run.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* run command: load raw binary from a FAT filesystem to 0x80000 and execute.
  2. * Usage: run /<device>/<file>. */
  3. #include "fbtext.h"
  4. #include "io.h"
  5. #include "run_api.h"
  6. #include "serial.h"
  7. #include "timer.h"
  8. #include "vga.h"
  9. #include "vfs.h"
  10. #define PROGRAM_LOAD_ADDR 0x80000
  11. #define PROGRAM_LOAD_MAX 0x6000 /* up to 0x86000, below the VGA window */
  12. /* User programs embedded in the kernel image: they travel with
  13. * `make dev`/`make net-dev`, so a board with no readable filesystem can
  14. * still run them. Rebuilt from prog.c/space.c by the Makefile. */
  15. extern unsigned char _binary_prog_bin_start[], _binary_prog_bin_end[];
  16. extern unsigned char _binary_space_bin_start[], _binary_space_bin_end[];
  17. struct embedded_prog {
  18. const char *name;
  19. const unsigned char *start;
  20. const unsigned char *end;
  21. };
  22. static const struct embedded_prog embedded_progs[] = {
  23. { "prog", _binary_prog_bin_start, _binary_prog_bin_end },
  24. { "space", _binary_space_bin_start, _binary_space_bin_end },
  25. };
  26. static int name_eq(const char *a, const char *b)
  27. {
  28. unsigned int i = 0;
  29. while (a[i] && b[i] && a[i] == b[i])
  30. i++;
  31. return a[i] == '\0' && b[i] == '\0';
  32. }
  33. /* Install the program API and hand control to the image at PROGRAM_LOAD_ADDR. */
  34. static void jump_to_program(void)
  35. {
  36. struct prog_api api = {
  37. .draw_string_xy = fbtext_draw_string_xy,
  38. .get_cols = fbtext_get_cols,
  39. .get_rows = fbtext_get_rows,
  40. .poll_key = io_poll_key,
  41. .poll_key_with_hw = io_poll_key_with_hw,
  42. .get_fb_width = vga_fb_width,
  43. .get_fb_height = vga_fb_height,
  44. .clear_rgb = vga_clear_rgb,
  45. .put_pixel_rgb = vga_put_pixel_rgb,
  46. .get_ticks_ms = get_ticks_ms,
  47. };
  48. io_drain_keyboard();
  49. *(struct prog_api *)PROGRAM_API_ADDR = api;
  50. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  51. }
  52. /* Load the named embedded program and run it (0 = name not found). */
  53. static int run_embedded(const char *name)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < sizeof(embedded_progs) / sizeof(embedded_progs[0]); i++) {
  57. const struct embedded_prog *p = &embedded_progs[i];
  58. unsigned int size;
  59. unsigned char *dst = (unsigned char *)PROGRAM_LOAD_ADDR;
  60. const unsigned char *src;
  61. if (!name_eq(name, p->name))
  62. continue;
  63. size = (unsigned int)(p->end - p->start);
  64. if (size == 0 || size > PROGRAM_LOAD_MAX) {
  65. io_putstr("run: embedded image size out of range\n");
  66. return 1;
  67. }
  68. for (src = p->start; src < p->end; )
  69. *dst++ = *src++;
  70. jump_to_program();
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. void program_run_main(const char *filename)
  76. {
  77. struct vfs_file f;
  78. struct vfs_mount *m = 0;
  79. unsigned int i;
  80. int r;
  81. if (!filename || !filename[0]) {
  82. io_putstr("run: usage run <name>|/<device>/<file>\n");
  83. return;
  84. }
  85. /* Names of embedded programs first: `run prog`, `run space`. */
  86. if (run_embedded(filename))
  87. return;
  88. /* Anything else comes from a mounted filesystem (FAT or the ramfs). */
  89. r = vfs_load_file(filename, &f, &m);
  90. if (r == VFS_ENOENT) {
  91. io_putstr("run: file not found\n");
  92. return;
  93. }
  94. if (r != 0) {
  95. io_putstr("run: disk read error (see above)\n");
  96. return;
  97. }
  98. if (f.size == 0 || f.size > PROGRAM_LOAD_MAX) {
  99. io_putstr("run: file too large\n");
  100. vfs_release_file(m, &f);
  101. return;
  102. }
  103. for (i = 0; i < f.size; i++)
  104. ((unsigned char *)PROGRAM_LOAD_ADDR)[i] =
  105. ((const unsigned char *)f.data)[i];
  106. vfs_release_file(m, &f);
  107. io_drain_keyboard();
  108. {
  109. struct prog_api api = {
  110. .draw_string_xy = fbtext_draw_string_xy,
  111. .get_cols = fbtext_get_cols,
  112. .get_rows = fbtext_get_rows,
  113. .poll_key = io_poll_key,
  114. .poll_key_with_hw = io_poll_key_with_hw,
  115. .get_fb_width = vga_fb_width,
  116. .get_fb_height = vga_fb_height,
  117. .clear_rgb = vga_clear_rgb,
  118. .put_pixel_rgb = vga_put_pixel_rgb,
  119. .get_ticks_ms = get_ticks_ms,
  120. };
  121. *(struct prog_api *)PROGRAM_API_ADDR = api;
  122. }
  123. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  124. }
  125. /* run serial: execute the binary already loaded at 0x80000 via serial_recv. */
  126. void program_run_serial_main(const char *arg)
  127. {
  128. (void)arg;
  129. if (serial_recv_size == 0) {
  130. io_putstr("run serial: no binary received (use 'serial recv' first)\n");
  131. return;
  132. }
  133. jump_to_program();
  134. }
  135. /* Command entry for the embedded programs (`prog`, `space`). */
  136. void program_run_embedded_main(const char *name)
  137. {
  138. if (!run_embedded(name))
  139. io_putstr("run: no such embedded program\n");
  140. }