run.c 4.3 KB

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