run.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* run command: load raw binary from a FAT filesystem to 0x31000 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 0x31000
  12. #define PROGRAM_LOAD_MAX 0x10000
  13. void program_run_main(const char *filename)
  14. {
  15. fat_fs_t *fs;
  16. const char *fs_path;
  17. unsigned int dir_cluster;
  18. char name_8_3[12];
  19. int n;
  20. if (!filename || !filename[0]) {
  21. io_putstr("run: usage run /<device>/<file>\n");
  22. return;
  23. }
  24. vfs_mount_all();
  25. if (vfs_resolve(filename, &fs, &fs_path) != 0) {
  26. io_putstr("run: unknown device or invalid path\n");
  27. return;
  28. }
  29. n = fat_resolve_path(fs, fs_path, &dir_cluster, name_8_3);
  30. if (n == -2) {
  31. io_putstr("run: disk read error (see above)\n");
  32. return;
  33. }
  34. if (n != 0) {
  35. io_putstr("run: file not found\n");
  36. return;
  37. }
  38. n = fat_read_file_in(fs, dir_cluster, name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
  39. if (n == -2) {
  40. io_putstr("run: disk read error (see above)\n");
  41. return;
  42. }
  43. if (n <= 0) {
  44. io_putstr("run: file not found\n");
  45. return;
  46. }
  47. io_drain_keyboard();
  48. {
  49. struct prog_api api = {
  50. .draw_string_xy = fbtext_draw_string_xy,
  51. .get_cols = fbtext_get_cols,
  52. .get_rows = fbtext_get_rows,
  53. .poll_key = io_poll_key,
  54. .poll_key_with_hw = io_poll_key_with_hw,
  55. .get_fb_width = vga_fb_width,
  56. .get_fb_height = vga_fb_height,
  57. .clear_rgb = vga_clear_rgb,
  58. .put_pixel_rgb = vga_put_pixel_rgb,
  59. .get_ticks_ms = get_ticks_ms,
  60. };
  61. *(struct prog_api *)PROGRAM_API_ADDR = api;
  62. }
  63. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  64. }
  65. /* run serial: execute the binary already loaded at 0x31000 via serial_recv. */
  66. void program_run_serial_main(const char *arg)
  67. {
  68. (void)arg;
  69. if (serial_recv_size == 0) {
  70. io_putstr("run serial: no binary received (use 'serial recv' first)\n");
  71. return;
  72. }
  73. io_drain_keyboard();
  74. {
  75. struct prog_api api = {
  76. .draw_string_xy = fbtext_draw_string_xy,
  77. .get_cols = fbtext_get_cols,
  78. .get_rows = fbtext_get_rows,
  79. .poll_key = io_poll_key,
  80. .poll_key_with_hw = io_poll_key_with_hw,
  81. .get_fb_width = vga_fb_width,
  82. .get_fb_height = vga_fb_height,
  83. .clear_rgb = vga_clear_rgb,
  84. .put_pixel_rgb = vga_put_pixel_rgb,
  85. .get_ticks_ms = get_ticks_ms,
  86. };
  87. *(struct prog_api *)PROGRAM_API_ADDR = api;
  88. }
  89. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  90. }