freedos.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* chainloader.c - boot another boot loader */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2007,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/loader.h>
  20. #include <grub/file.h>
  21. #include <grub/err.h>
  22. #include <grub/device.h>
  23. #include <grub/disk.h>
  24. #include <grub/misc.h>
  25. #include <grub/types.h>
  26. #include <grub/partition.h>
  27. #include <grub/dl.h>
  28. #include <grub/command.h>
  29. #include <grub/machine/biosnum.h>
  30. #include <grub/i18n.h>
  31. #include <grub/video.h>
  32. #include <grub/mm.h>
  33. #include <grub/cpu/relocator.h>
  34. #include <grub/machine/chainloader.h>
  35. GRUB_MOD_LICENSE ("GPLv3+");
  36. static grub_dl_t my_mod;
  37. static struct grub_relocator *rel;
  38. static grub_uint32_t ebx = 0xffffffff;
  39. #define GRUB_FREEDOS_SEGMENT 0x60
  40. #define GRUB_FREEDOS_ADDR (GRUB_FREEDOS_SEGMENT << 4)
  41. #define GRUB_FREEDOS_STACK_SEGMENT 0x1fe0
  42. #define GRUB_FREEDOS_STACK_BPB_POINTER 0x7c00
  43. #define GRUB_FREEDOS_BPB_ADDR ((GRUB_FREEDOS_STACK_SEGMENT << 4) \
  44. + GRUB_FREEDOS_STACK_BPB_POINTER)
  45. /* FreeDOS boot.asm passes register sp as exactly this. Importantly,
  46. it must point below the BPB (to avoid overwriting any of it). */
  47. #define GRUB_FREEDOS_STACK_POINTER (GRUB_FREEDOS_STACK_BPB_POINTER \
  48. - 0x60)
  49. /* In this, the additional 8192 bytes are the stack reservation; the
  50. remaining parts trivially give the maximum allowed size. */
  51. #define GRUB_FREEDOS_MAX_SIZE ((GRUB_FREEDOS_STACK_SEGMENT << 4) \
  52. + GRUB_FREEDOS_STACK_POINTER \
  53. - GRUB_FREEDOS_ADDR \
  54. - 8192)
  55. static grub_err_t
  56. grub_freedos_boot (void)
  57. {
  58. struct grub_relocator16_state state = {
  59. .cs = GRUB_FREEDOS_SEGMENT,
  60. .ip = 0,
  61. .ds = GRUB_FREEDOS_STACK_SEGMENT,
  62. .es = 0,
  63. .fs = 0,
  64. .gs = 0,
  65. .ss = GRUB_FREEDOS_STACK_SEGMENT,
  66. .sp = GRUB_FREEDOS_STACK_POINTER,
  67. .ebp = GRUB_FREEDOS_STACK_BPB_POINTER,
  68. .ebx = ebx,
  69. .edx = ebx,
  70. .a20 = 1
  71. };
  72. grub_video_set_mode ("text", 0, 0);
  73. return grub_relocator16_boot (rel, state);
  74. }
  75. static grub_err_t
  76. grub_freedos_unload (void)
  77. {
  78. grub_relocator_unload (rel);
  79. rel = NULL;
  80. grub_dl_unref (my_mod);
  81. return GRUB_ERR_NONE;
  82. }
  83. static grub_err_t
  84. grub_cmd_freedos (grub_command_t cmd __attribute__ ((unused)),
  85. int argc, char *argv[])
  86. {
  87. grub_file_t file = 0;
  88. grub_err_t err;
  89. void *bs, *kernelsys;
  90. grub_size_t kernelsyssize;
  91. grub_device_t dev;
  92. if (argc == 0)
  93. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  94. grub_dl_ref (my_mod);
  95. rel = grub_relocator_new ();
  96. if (!rel)
  97. goto fail;
  98. file = grub_file_open (argv[0], GRUB_FILE_TYPE_FREEDOS);
  99. if (! file)
  100. goto fail;
  101. {
  102. grub_relocator_chunk_t ch;
  103. err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_FREEDOS_BPB_ADDR,
  104. GRUB_DISK_SECTOR_SIZE);
  105. if (err)
  106. goto fail;
  107. bs = get_virtual_current_address (ch);
  108. }
  109. ebx = grub_get_root_biosnumber ();
  110. dev = grub_device_open (0);
  111. if (dev && dev->disk)
  112. {
  113. err = grub_disk_read (dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, bs);
  114. if (err)
  115. {
  116. grub_device_close (dev);
  117. goto fail;
  118. }
  119. grub_chainloader_patch_bpb (bs, dev, ebx);
  120. }
  121. if (dev)
  122. grub_device_close (dev);
  123. kernelsyssize = grub_file_size (file);
  124. if (kernelsyssize > GRUB_FREEDOS_MAX_SIZE)
  125. {
  126. grub_error (GRUB_ERR_BAD_OS,
  127. N_("the size of `%s' is too large"), argv[0]);
  128. goto fail;
  129. }
  130. {
  131. grub_relocator_chunk_t ch;
  132. err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_FREEDOS_ADDR,
  133. kernelsyssize);
  134. if (err)
  135. goto fail;
  136. kernelsys = get_virtual_current_address (ch);
  137. }
  138. if (grub_file_read (file, kernelsys, kernelsyssize)
  139. != (grub_ssize_t) kernelsyssize)
  140. goto fail;
  141. grub_loader_set (grub_freedos_boot, grub_freedos_unload, 1);
  142. return GRUB_ERR_NONE;
  143. fail:
  144. if (file)
  145. grub_file_close (file);
  146. grub_freedos_unload ();
  147. return grub_errno;
  148. }
  149. static grub_command_t cmd;
  150. GRUB_MOD_INIT(freedos)
  151. {
  152. cmd = grub_register_command ("freedos", grub_cmd_freedos,
  153. 0, N_("Load FreeDOS kernel.sys."));
  154. my_mod = mod;
  155. }
  156. GRUB_MOD_FINI(freedos)
  157. {
  158. grub_unregister_command (cmd);
  159. }