nativedisk.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_EFIDISK_ID:
  62. case GRUB_DISK_DEVICE_NAND_ID:
  63. case GRUB_DISK_DEVICE_ARCDISK_ID:
  64. case GRUB_DISK_DEVICE_HOSTDISK_ID:
  65. case GRUB_DISK_DEVICE_UBOOTDISK_ID:
  66. break;
  67. /* Native disks. */
  68. case GRUB_DISK_DEVICE_ATA_ID:
  69. case GRUB_DISK_DEVICE_SCSI_ID:
  70. case GRUB_DISK_DEVICE_XEN:
  71. if (getnative)
  72. break;
  73. /* Virtual disks. */
  74. /* GRUB dynamically generated files. */
  75. case GRUB_DISK_DEVICE_PROCFS_ID:
  76. /* To access through host OS routines (grub-emu only). */
  77. case GRUB_DISK_DEVICE_HOST_ID:
  78. /* To access coreboot roms. */
  79. case GRUB_DISK_DEVICE_CBFSDISK_ID:
  80. /* GRUB-only memdisk. Can't match any of firmware devices. */
  81. case GRUB_DISK_DEVICE_MEMDISK_ID:
  82. grub_dprintf ("nativedisk", "Skipping native disk %s\n",
  83. dev->disk->name);
  84. grub_device_close (dev);
  85. return 0;
  86. /* FIXME: those probably need special handling. */
  87. case GRUB_DISK_DEVICE_LOOPBACK_ID:
  88. case GRUB_DISK_DEVICE_DISKFILTER_ID:
  89. case GRUB_DISK_DEVICE_CRYPTODISK_ID:
  90. break;
  91. }
  92. if (dev)
  93. fs = grub_fs_probe (dev);
  94. if (!fs)
  95. {
  96. grub_device_close (dev);
  97. return grub_errno;
  98. }
  99. if (!fs->uuid || fs->uuid (dev, uuid) || !*uuid)
  100. {
  101. grub_device_close (dev);
  102. if (!grub_errno)
  103. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  104. N_("%s does not support UUIDs"), fs->name);
  105. return grub_errno;
  106. }
  107. grub_device_close (dev);
  108. return GRUB_ERR_NONE;
  109. }
  110. struct search_ctx
  111. {
  112. char *root_uuid;
  113. char *prefix_uuid;
  114. const char *prefix_path;
  115. int prefix_found, root_found;
  116. };
  117. static int
  118. iterate_device (const char *name, void *data)
  119. {
  120. struct search_ctx *ctx = data;
  121. char *cur_uuid;
  122. if (get_uuid (name, &cur_uuid, 1))
  123. {
  124. if (grub_errno == GRUB_ERR_UNKNOWN_FS)
  125. grub_errno = 0;
  126. grub_print_error ();
  127. return 0;
  128. }
  129. grub_dprintf ("nativedisk", "checking %s: %s\n", name,
  130. cur_uuid);
  131. if (ctx->prefix_uuid && grub_strcasecmp (cur_uuid, ctx->prefix_uuid) == 0)
  132. {
  133. char *prefix;
  134. prefix = grub_xasprintf ("(%s)/%s", name, ctx->prefix_path);
  135. grub_env_set ("prefix", prefix);
  136. grub_free (prefix);
  137. ctx->prefix_found = 1;
  138. }
  139. if (ctx->root_uuid && grub_strcasecmp (cur_uuid, ctx->root_uuid) == 0)
  140. {
  141. grub_env_set ("root", name);
  142. ctx->root_found = 1;
  143. }
  144. return ctx->prefix_found && ctx->root_found;
  145. }
  146. static grub_err_t
  147. grub_cmd_nativedisk (grub_command_t cmd __attribute__ ((unused)),
  148. int argc, char **args_in)
  149. {
  150. char *uuid_root = 0, *uuid_prefix, *prefdev = 0;
  151. const char *prefix = 0;
  152. const char *path_prefix = 0;
  153. int mods_loaded = 0;
  154. grub_dl_t *mods;
  155. const char **args;
  156. int i;
  157. if (argc == 0)
  158. {
  159. argc = ARRAY_SIZE (modnames_def);
  160. args = modnames_def;
  161. }
  162. else
  163. args = (const char **) args_in;
  164. prefix = grub_env_get ("prefix");
  165. if (! prefix)
  166. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
  167. if (prefix)
  168. path_prefix = (prefix[0] == '(') ? grub_strchr (prefix, ')') : NULL;
  169. if (path_prefix)
  170. path_prefix++;
  171. else
  172. path_prefix = prefix;
  173. mods = grub_malloc (argc * sizeof (mods[0]));
  174. if (!mods)
  175. return grub_errno;
  176. if (get_uuid (NULL, &uuid_root, 0))
  177. {
  178. grub_free (mods);
  179. return grub_errno;
  180. }
  181. prefdev = grub_file_get_device_name (prefix);
  182. if (grub_errno)
  183. {
  184. grub_print_error ();
  185. prefdev = 0;
  186. }
  187. if (get_uuid (prefdev, &uuid_prefix, 0))
  188. {
  189. grub_free (uuid_root);
  190. grub_free (prefdev);
  191. grub_free (mods);
  192. return grub_errno;
  193. }
  194. grub_dprintf ("nativedisk", "uuid_prefix = %s, uuid_root = %s\n",
  195. uuid_prefix, uuid_root);
  196. for (mods_loaded = 0; mods_loaded < argc; mods_loaded++)
  197. {
  198. char *filename;
  199. grub_dl_t mod;
  200. grub_file_t file = NULL;
  201. grub_ssize_t size;
  202. void *core = 0;
  203. mod = grub_dl_get (args[mods_loaded]);
  204. if (mod)
  205. {
  206. mods[mods_loaded] = 0;
  207. continue;
  208. }
  209. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
  210. prefix, args[mods_loaded]);
  211. if (! filename)
  212. goto fail;
  213. file = grub_file_open (filename);
  214. grub_free (filename);
  215. if (! file)
  216. goto fail;
  217. size = grub_file_size (file);
  218. core = grub_malloc (size);
  219. if (! core)
  220. {
  221. grub_file_close (file);
  222. goto fail;
  223. }
  224. if (grub_file_read (file, core, size) != (grub_ssize_t) size)
  225. {
  226. grub_file_close (file);
  227. grub_free (core);
  228. goto fail;
  229. }
  230. grub_file_close (file);
  231. mods[mods_loaded] = grub_dl_load_core_noinit (core, size);
  232. if (! mods[mods_loaded])
  233. goto fail;
  234. }
  235. for (i = 0; i < argc; i++)
  236. if (mods[i])
  237. grub_dl_init (mods[i]);
  238. if (uuid_prefix || uuid_root)
  239. {
  240. struct search_ctx ctx;
  241. grub_fs_autoload_hook_t saved_autoload;
  242. /* No need to autoload FS since obviously we already have the necessary fs modules. */
  243. saved_autoload = grub_fs_autoload_hook;
  244. grub_fs_autoload_hook = 0;
  245. ctx.root_uuid = uuid_root;
  246. ctx.prefix_uuid = uuid_prefix;
  247. ctx.prefix_path = path_prefix;
  248. ctx.prefix_found = !uuid_prefix;
  249. ctx.root_found = !uuid_root;
  250. /* FIXME: try to guess the correct values. */
  251. grub_device_iterate (iterate_device, &ctx);
  252. grub_fs_autoload_hook = saved_autoload;
  253. }
  254. grub_free (uuid_root);
  255. grub_free (uuid_prefix);
  256. grub_free (prefdev);
  257. grub_free (mods);
  258. return GRUB_ERR_NONE;
  259. fail:
  260. grub_free (uuid_root);
  261. grub_free (uuid_prefix);
  262. grub_free (prefdev);
  263. for (i = 0; i < mods_loaded; i++)
  264. if (mods[i])
  265. {
  266. mods[i]->fini = 0;
  267. grub_dl_unload (mods[i]);
  268. }
  269. grub_free (mods);
  270. return grub_errno;
  271. }
  272. static grub_command_t cmd;
  273. GRUB_MOD_INIT(nativedisk)
  274. {
  275. cmd = grub_register_command ("nativedisk", grub_cmd_nativedisk, N_("[MODULE1 MODULE2 ...]"),
  276. N_("Switch to native disk drivers. If no modules are specified default set (pata,ahci,usbms,ohci,uhci,ehci) is used"));
  277. }
  278. GRUB_MOD_FINI(nativedisk)
  279. {
  280. grub_unregister_command (cmd);
  281. }