pxechainloader.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* chainloader.c - boot another boot loader */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2007,2009,2010,2012 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/pxe.h>
  35. #include <grub/net.h>
  36. static grub_dl_t my_mod;
  37. static struct grub_relocator *rel;
  38. static grub_uint32_t edx = 0xffffffff;
  39. static char boot_file[128];
  40. static char server_name[64];
  41. GRUB_MOD_LICENSE ("GPLv3+");
  42. static grub_err_t
  43. grub_pxechain_boot (void)
  44. {
  45. struct grub_relocator16_state state = {
  46. .cs = 0,
  47. .ip = 0x7c00,
  48. .ds = 0,
  49. .es = 0,
  50. .fs = 0,
  51. .gs = 0,
  52. .ss = 0,
  53. .sp = 0x7c00,
  54. .edx = edx
  55. };
  56. struct grub_net_bootp_packet *bp;
  57. bp = grub_pxe_get_cached (GRUB_PXENV_PACKET_TYPE_DHCP_ACK);
  58. grub_video_set_mode ("text", 0, 0);
  59. if (bp && boot_file[0])
  60. grub_memcpy (bp->boot_file, boot_file, sizeof (bp->boot_file));
  61. if (bp && server_name[0])
  62. grub_memcpy (bp->server_name, server_name, sizeof (bp->server_name));
  63. return grub_relocator16_boot (rel, state);
  64. }
  65. static grub_err_t
  66. grub_pxechain_unload (void)
  67. {
  68. grub_relocator_unload (rel);
  69. rel = NULL;
  70. grub_dl_unref (my_mod);
  71. return GRUB_ERR_NONE;
  72. }
  73. static grub_err_t
  74. grub_cmd_pxechain (grub_command_t cmd __attribute__ ((unused)),
  75. int argc, char *argv[])
  76. {
  77. grub_file_t file = 0;
  78. grub_err_t err;
  79. void *image;
  80. grub_size_t imagesize;
  81. char *fname;
  82. if (argc == 0)
  83. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  84. grub_dl_ref (my_mod);
  85. rel = grub_relocator_new ();
  86. if (!rel)
  87. goto fail;
  88. file = grub_file_open (argv[0], GRUB_FILE_TYPE_PXECHAINLOADER);
  89. if (! file)
  90. goto fail;
  91. if (file->device->net && file->device->net->name)
  92. fname = file->device->net->name;
  93. else
  94. {
  95. fname = argv[0];
  96. if (fname[0] == '(')
  97. {
  98. fname = grub_strchr (fname, ')');
  99. if (fname)
  100. fname++;
  101. else
  102. fname = argv[0];
  103. }
  104. }
  105. grub_memset (boot_file, 0, sizeof (boot_file));
  106. grub_strncpy (boot_file, fname, sizeof (boot_file));
  107. grub_memset (server_name, 0, sizeof (server_name));
  108. if (file->device->net && file->device->net->server)
  109. grub_strncpy (server_name, file->device->net->server, sizeof (server_name));
  110. edx = grub_get_root_biosnumber ();
  111. imagesize = grub_file_size (file);
  112. {
  113. grub_relocator_chunk_t ch;
  114. err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7c00, imagesize);
  115. if (err)
  116. goto fail;
  117. image = get_virtual_current_address (ch);
  118. }
  119. if (grub_file_read (file, image, imagesize) != (grub_ssize_t) imagesize)
  120. goto fail;
  121. grub_loader_set (grub_pxechain_boot, grub_pxechain_unload,
  122. GRUB_LOADER_FLAG_NORETURN | GRUB_LOADER_FLAG_PXE_NOT_UNLOAD);
  123. return GRUB_ERR_NONE;
  124. fail:
  125. if (file)
  126. grub_file_close (file);
  127. grub_pxechain_unload ();
  128. return grub_errno;
  129. }
  130. static grub_command_t cmd;
  131. GRUB_MOD_INIT(pxechainloader)
  132. {
  133. cmd = grub_register_command ("pxechainloader", grub_cmd_pxechain,
  134. 0, N_("Load a PXE image."));
  135. my_mod = mod;
  136. }
  137. GRUB_MOD_FINI(pxechainloader)
  138. {
  139. grub_unregister_command (cmd);
  140. }