run.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* run command: load raw binary from FAT floppy to 0x21000 and execute. */
  2. #include "fat.h"
  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. #define PROGRAM_LOAD_ADDR 0x21000
  10. #define PROGRAM_LOAD_MAX 0x10000
  11. #define NAME_BUF_SIZE 12
  12. static void to_8_3(const char *in, unsigned int in_len, char *out)
  13. {
  14. unsigned int i, j, dot;
  15. for (i = 0; i < in_len && in[i] == ' '; i++)
  16. ;
  17. dot = i;
  18. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  19. dot++;
  20. j = 0;
  21. while (i < dot && j < 8) {
  22. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  23. i++;
  24. }
  25. while (j < 8)
  26. out[j++] = ' ';
  27. if (i < in_len && in[i] == '.')
  28. i++;
  29. while (i < in_len && in[i] != ' ' && j < 11) {
  30. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  31. i++;
  32. }
  33. while (j < 11)
  34. out[j++] = ' ';
  35. out[11] = '\0';
  36. }
  37. void program_run_main(const char *filename)
  38. {
  39. char name_8_3[NAME_BUF_SIZE];
  40. int n;
  41. if (!filename || !filename[0]) {
  42. io_putstr("run: usage run <filename>\n");
  43. return;
  44. }
  45. to_8_3(filename, 64, name_8_3);
  46. n = fat_init();
  47. if (n == -2) {
  48. io_putstr("run: disk read error (see above)\n");
  49. return;
  50. }
  51. if (n != 0) {
  52. io_putstr("run: no FAT filesystem found\n");
  53. return;
  54. }
  55. n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
  56. if (n == -2) {
  57. io_putstr("run: disk read error (see above)\n");
  58. return;
  59. }
  60. if (n <= 0) {
  61. io_putstr("run: file not found\n");
  62. return;
  63. }
  64. io_drain_keyboard();
  65. {
  66. struct prog_api api = {
  67. .draw_string_xy = fbtext_draw_string_xy,
  68. .get_cols = fbtext_get_cols,
  69. .get_rows = fbtext_get_rows,
  70. .poll_key = io_poll_key,
  71. .poll_key_with_hw = io_poll_key_with_hw,
  72. .get_fb_width = vga_fb_width,
  73. .get_fb_height = vga_fb_height,
  74. .clear_rgb = vga_clear_rgb,
  75. .put_pixel_rgb = vga_put_pixel_rgb,
  76. .get_ticks_ms = get_ticks_ms,
  77. };
  78. *(struct prog_api *)PROGRAM_API_ADDR = api;
  79. }
  80. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  81. }
  82. /* run serial: execute the binary already loaded at 0x21000 via serial_recv. */
  83. void program_run_serial_main(const char *arg)
  84. {
  85. (void)arg;
  86. if (serial_recv_size == 0) {
  87. io_putstr("run serial: no binary received (use 'serial recv' first)\n");
  88. return;
  89. }
  90. io_drain_keyboard();
  91. {
  92. struct prog_api api = {
  93. .draw_string_xy = fbtext_draw_string_xy,
  94. .get_cols = fbtext_get_cols,
  95. .get_rows = fbtext_get_rows,
  96. .poll_key = io_poll_key,
  97. .poll_key_with_hw = io_poll_key_with_hw,
  98. .get_fb_width = vga_fb_width,
  99. .get_fb_height = vga_fb_height,
  100. .clear_rgb = vga_clear_rgb,
  101. .put_pixel_rgb = vga_put_pixel_rgb,
  102. .get_ticks_ms = get_ticks_ms,
  103. };
  104. *(struct prog_api *)PROGRAM_API_ADDR = api;
  105. }
  106. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  107. }