run.c 2.4 KB

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