nativedisk.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/dl.h>
  23. #include <grub/command.h>
  24. #include <grub/i18n.h>
  25. #include <grub/device.h>
  26. #include <grub/mm.h>
  27. #include <grub/fs.h>
  28. #include <grub/env.h>
  29. #include <grub/file.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. static const char *modnames_def[] = {
  32. /* FIXME: autogenerate this. */
  33. #if defined (__i386__) || defined (__x86_64__) || defined (GRUB_MACHINE_MIPS_LOONGSON)
  34. "pata", "ahci", "usbms", "ohci", "uhci", "ehci"
  35. #elif defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
  36. "pata"
  37. #else
  38. #error "Fill this"
  39. #endif
  40. };
  41. static grub_err_t
  42. get_uuid (const char *name, char **uuid, int getnative)
  43. {
  44. grub_device_t dev;
  45. grub_fs_t fs = 0;
  46. *uuid = 0;
  47. dev = grub_device_open (name);
  48. if (!dev)
  49. return grub_errno;
  50. if (!dev->disk)
  51. {
  52. grub_dprintf ("nativedisk", "Skipping non-disk\n");
  53. grub_device_close (dev);
  54. return 0;
  55. }
  56. switch (dev->disk->dev->id)
  57. {
  58. /* Firmware disks. */
  59. case GRUB_DISK_DEVICE_BIOSDISK_ID:
  60. case GRUB_DISK_DEVICE_OFDISK_ID:
  61. case GRUB_DISK_DEVICE_OBDISK_ID:
  62. case GRUB_DISK_DEVICE_EFIDISK_ID:
  63. case GRUB_DISK_DEVICE_NAND_ID:
  64. case GRUB_DISK_DEVICE_ARCDISK_ID:
  65. case GRUB_DISK_DEVICE_HOSTDISK_ID:
  66. case GRUB_DISK_DEVICE_UBOOTDISK_ID:
  67. break;
  68. /* Native disks. */
  69. case GRUB_DISK_DEVICE_ATA_ID:
  70. case GRUB_DISK_DEVICE_SCSI_ID:
  71. case GRUB_DISK_DEVICE_XEN:
  72. if (getnative)
  73. break;
  74. /* FALLTHROUGH */
  75. /* Virtual disks. */
  76. /* GRUB dynamically generated files. */
  77. case GRUB_DISK_DEVICE_PROCFS_ID:
  78. /* To access through host OS routines (grub-emu only). */
  79. case GRUB_DISK_DEVICE_HOST_ID:
  80. /* To access coreboot roms. */
  81. case GRUB_DISK_DEVICE_CBFSDISK_ID:
  82. /* GRUB-only memdisk. Can't match any of firmware devices. */
  83. case GRUB_DISK_DEVICE_MEMDISK_ID:
  84. grub_dprintf ("nativedisk", "Skipping native disk %s\n",
  85. dev->disk->name);
  86. grub_device_close (dev);
  87. return 0;
  88. /* FIXME: those probably need special handling. */
  89. case GRUB_DISK_DEVICE_LOOPBACK_ID:
  90. case GRUB_DISK_DEVICE_DISKFILTER_ID:
  91. case GRUB_DISK_DEVICE_CRYPTODISK_ID:
  92. break;
  93. }
  94. if (dev)
  95. fs = grub_fs_probe (dev);
  96. if (!fs)
  97. {
  98. grub_device_close (dev);
  99. return grub_errno;
  100. }
  101. if (!fs->fs_uuid || fs->fs_uuid (dev, uuid) || !*uuid)
  102. {
  103. grub_device_close (dev);
  104. if (!grub_errno)
  105. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  106. N_("%s does not support UUIDs"), fs->name);
  107. return grub_errno;
  108. }
  109. grub_device_close (dev);
  110. return GRUB_ERR_NONE;
  111. }
  112. struct search_ctx
  113. {
  114. char *root_uuid;
  115. char *prefix_uuid;
  116. const char *prefix_path;
  117. int prefix_found, root_found;
  118. };
  119. static int
  120. iterate_device (const char *name, void *data)
  121. {
  122. struct search_ctx *ctx = data;
  123. char *cur_uuid;
  124. if (get_uuid (name, &cur_uuid, 1))
  125. {
  126. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  127. grub_errno = 0;
  128. grub_print_error ();
  129. return 0;
  130. }
  131. grub_dprintf ("nativedisk", "checking %s: %s\n", name,
  132. cur_uuid);
  133. if (ctx->prefix_uuid && grub_strcasecmp (cur_uuid, ctx->prefix_uuid) == 0)
  134. {
  135. char *prefix;
  136. prefix = grub_xasprintf ("(%s)/%s", name, ctx->prefix_path);
  137. grub_env_set ("prefix", prefix);
  138. grub_free (prefix);
  139. ctx->prefix_found = 1;
  140. }
  141. if (ctx->root_uuid && grub_strcasecmp (cur_uuid, ctx->root_uuid) == 0)
  142. {
  143. grub_env_set ("root", name);
  144. ctx->root_found = 1;
  145. }
  146. return ctx->prefix_found && ctx->root_found;
  147. }
  148. static grub_err_t
  149. grub_cmd_nativedisk (grub_command_t cmd __attribute__ ((unused)),
  150. int argc, char **args_in)
  151. {
  152. char *uuid_root = 0, *uuid_prefix, *prefdev = 0;
  153. const char *prefix = 0;
  154. const char *path_prefix = 0;
  155. int mods_loaded = 0;
  156. grub_dl_t *mods;
  157. const char **args;
  158. int i;
  159. if (argc == 0)
  160. {
  161. argc = ARRAY_SIZE (modnames_def);
  162. args = modnames_def;
  163. }
  164. else
  165. args = (const char **) args_in;
  166. prefix = grub_env_get ("prefix");
  167. if (! prefix)
  168. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
  169. if (prefix)
  170. path_prefix = (prefix[0] == '(') ? grub_strchr (prefix, ')') : NULL;
  171. if (path_prefix)
  172. path_prefix++;
  173. else
  174. path_prefix = prefix;
  175. mods = grub_calloc (argc, sizeof (mods[0]));
  176. if (!mods)
  177. return grub_errno;
  178. if (get_uuid (NULL, &uuid_root, 0))
  179. {
  180. grub_free (mods);
  181. return grub_errno;
  182. }
  183. prefdev = grub_file_get_device_name (prefix);
  184. if (grub_errno)
  185. {
  186. grub_print_error ();
  187. prefdev = 0;
  188. }
  189. if (get_uuid (prefdev, &uuid_prefix, 0))
  190. {
  191. grub_free (uuid_root);
  192. grub_free (prefdev);
  193. grub_free (mods);
  194. return grub_errno;
  195. }
  196. grub_dprintf ("nativedisk", "uuid_prefix = %s, uuid_root = %s\n",
  197. uuid_prefix, uuid_root);
  198. for (mods_loaded = 0; mods_loaded < argc; mods_loaded++)
  199. {
  200. char *filename;
  201. grub_dl_t mod;
  202. grub_file_t file = NULL;
  203. grub_ssize_t size;
  204. void *core = 0;
  205. mod = grub_dl_get (args[mods_loaded]);
  206. if (mod)
  207. {
  208. mods[mods_loaded] = 0;
  209. continue;
  210. }
  211. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
  212. prefix, args[mods_loaded]);
  213. if (! filename)
  214. goto fail;
  215. file = grub_file_open (filename,
  216. GRUB_FILE_TYPE_GRUB_MODULE);
  217. grub_free (filename);
  218. if (! file)
  219. goto fail;
  220. size = grub_file_size (file);
  221. core = grub_malloc (size);
  222. if (! core)
  223. {
  224. grub_file_close (file);
  225. goto fail;
  226. }
  227. if (grub_file_read (file, core, size) != (grub_ssize_t) size)
  228. {
  229. grub_file_close (file);
  230. grub_free (core);
  231. goto fail;
  232. }
  233. grub_file_close (file);
  234. mods[mods_loaded] = grub_dl_load_core_noinit (core, size);
  235. if (! mods[mods_loaded])
  236. goto fail;
  237. }
  238. for (i = 0; i < argc; i++)
  239. if (mods[i])
  240. grub_dl_init (mods[i]);
  241. if (uuid_prefix || uuid_root)
  242. {
  243. struct search_ctx ctx;
  244. grub_fs_autoload_hook_t saved_autoload;
  245. /* No need to autoload FS since obviously we already have the necessary fs modules. */
  246. saved_autoload = grub_fs_autoload_hook;
  247. grub_fs_autoload_hook = 0;
  248. ctx.root_uuid = uuid_root;
  249. ctx.prefix_uuid = uuid_prefix;
  250. ctx.prefix_path = path_prefix;
  251. ctx.prefix_found = !uuid_prefix;
  252. ctx.root_found = !uuid_root;
  253. /* FIXME: try to guess the correct values. */
  254. grub_device_iterate (iterate_device, &ctx);
  255. grub_fs_autoload_hook = saved_autoload;
  256. }
  257. grub_free (uuid_root);
  258. grub_free (uuid_prefix);
  259. grub_free (prefdev);
  260. grub_free (mods);
  261. return GRUB_ERR_NONE;
  262. fail:
  263. grub_free (uuid_root);
  264. grub_free (uuid_prefix);
  265. grub_free (prefdev);
  266. for (i = 0; i < mods_loaded; i++)
  267. if (mods[i])
  268. {
  269. mods[i]->fini = 0;
  270. grub_dl_unload (mods[i]);
  271. }
  272. grub_free (mods);
  273. return grub_errno;
  274. }
  275. static grub_command_t cmd;
  276. GRUB_MOD_INIT(nativedisk)
  277. {
  278. cmd = grub_register_command ("nativedisk", grub_cmd_nativedisk, N_("[MODULE1 MODULE2 ...]"),
  279. N_("Switch to native disk drivers. If no modules are specified default set (pata,ahci,usbms,ohci,uhci,ehci) is used"));
  280. }
  281. GRUB_MOD_FINI(nativedisk)
  282. {
  283. grub_unregister_command (cmd);
  284. }