nativedisk.c 7.8 KB

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