ntldr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 edx = 0xffffffff;
  39. #define GRUB_NTLDR_SEGMENT 0x2000
  40. static grub_err_t
  41. grub_ntldr_boot (void)
  42. {
  43. struct grub_relocator16_state state = {
  44. .cs = GRUB_NTLDR_SEGMENT,
  45. .ip = 0,
  46. .ds = 0,
  47. .es = 0,
  48. .fs = 0,
  49. .gs = 0,
  50. .ss = 0,
  51. .sp = 0x7c00,
  52. .edx = edx,
  53. .a20 = 1
  54. };
  55. grub_video_set_mode ("text", 0, 0);
  56. return grub_relocator16_boot (rel, state);
  57. }
  58. static grub_err_t
  59. grub_ntldr_unload (void)
  60. {
  61. grub_relocator_unload (rel);
  62. rel = NULL;
  63. grub_dl_unref (my_mod);
  64. return GRUB_ERR_NONE;
  65. }
  66. static grub_err_t
  67. grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)),
  68. int argc, char *argv[])
  69. {
  70. grub_file_t file = 0;
  71. grub_err_t err;
  72. void *bs, *ntldr;
  73. grub_size_t ntldrsize;
  74. grub_device_t dev;
  75. if (argc == 0)
  76. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  77. grub_dl_ref (my_mod);
  78. rel = grub_relocator_new ();
  79. if (!rel)
  80. goto fail;
  81. file = grub_file_open (argv[0], GRUB_FILE_TYPE_NTLDR);
  82. if (! file)
  83. goto fail;
  84. {
  85. grub_relocator_chunk_t ch;
  86. err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
  87. GRUB_DISK_SECTOR_SIZE);
  88. if (err)
  89. goto fail;
  90. bs = get_virtual_current_address (ch);
  91. }
  92. edx = grub_get_root_biosnumber ();
  93. dev = grub_device_open (0);
  94. if (dev && dev->disk)
  95. {
  96. err = grub_disk_read (dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, bs);
  97. if (err)
  98. {
  99. grub_device_close (dev);
  100. goto fail;
  101. }
  102. grub_chainloader_patch_bpb (bs, dev, edx);
  103. }
  104. if (dev)
  105. grub_device_close (dev);
  106. ntldrsize = grub_file_size (file);
  107. {
  108. grub_relocator_chunk_t ch;
  109. err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_NTLDR_SEGMENT << 4,
  110. ntldrsize);
  111. if (err)
  112. goto fail;
  113. ntldr = get_virtual_current_address (ch);
  114. }
  115. if (grub_file_read (file, ntldr, ntldrsize)
  116. != (grub_ssize_t) ntldrsize)
  117. goto fail;
  118. grub_loader_set (grub_ntldr_boot, grub_ntldr_unload, 1);
  119. return GRUB_ERR_NONE;
  120. fail:
  121. if (file)
  122. grub_file_close (file);
  123. grub_ntldr_unload ();
  124. return grub_errno;
  125. }
  126. static grub_command_t cmd;
  127. GRUB_MOD_INIT(ntldr)
  128. {
  129. cmd = grub_register_command ("ntldr", grub_cmd_ntldr,
  130. 0, N_("Load NTLDR or BootMGR."));
  131. my_mod = mod;
  132. }
  133. GRUB_MOD_FINI(ntldr)
  134. {
  135. grub_unregister_command (cmd);
  136. }