parttool.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* parttool.c - common dispatcher and parser for partition operations */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * This program 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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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 this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <grub/types.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/err.h>
  24. #include <grub/dl.h>
  25. #include <grub/normal.h>
  26. #include <grub/device.h>
  27. #include <grub/disk.h>
  28. #include <grub/partition.h>
  29. #include <grub/parttool.h>
  30. #include <grub/command.h>
  31. #include <grub/i18n.h>
  32. GRUB_MOD_LICENSE ("GPLv2+");
  33. static struct grub_parttool *parts = 0;
  34. static int curhandle = 0;
  35. static grub_dl_t mymod;
  36. static char helpmsg[] =
  37. N_("Perform COMMANDS on partition.\n"
  38. "Use `parttool PARTITION help' for the list "
  39. "of available commands.");
  40. int
  41. grub_parttool_register(const char *part_name,
  42. const grub_parttool_function_t func,
  43. const struct grub_parttool_argdesc *args)
  44. {
  45. struct grub_parttool *cur;
  46. int nargs = 0;
  47. if (! parts)
  48. grub_dl_ref (mymod);
  49. cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
  50. cur->next = parts;
  51. cur->name = grub_strdup (part_name);
  52. cur->handle = curhandle++;
  53. for (nargs = 0; args[nargs].name != 0; nargs++);
  54. cur->nargs = nargs;
  55. cur->args = (struct grub_parttool_argdesc *)
  56. grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
  57. grub_memcpy (cur->args, args,
  58. (nargs + 1) * sizeof (struct grub_parttool_argdesc));
  59. cur->func = func;
  60. parts = cur;
  61. return cur->handle;
  62. }
  63. void
  64. grub_parttool_unregister (int handle)
  65. {
  66. struct grub_parttool *prev = 0, *cur, *t;
  67. for (cur = parts; cur; )
  68. if (cur->handle == handle)
  69. {
  70. grub_free (cur->args);
  71. grub_free (cur->name);
  72. if (prev)
  73. prev->next = cur->next;
  74. else
  75. parts = cur->next;
  76. t = cur;
  77. cur = cur->next;
  78. grub_free (t);
  79. }
  80. else
  81. {
  82. prev = cur;
  83. cur = cur->next;
  84. }
  85. if (! parts)
  86. grub_dl_unref (mymod);
  87. }
  88. static grub_err_t
  89. show_help (grub_device_t dev)
  90. {
  91. int found = 0;
  92. struct grub_parttool *cur;
  93. for (cur = parts; cur; cur = cur->next)
  94. if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
  95. {
  96. struct grub_parttool_argdesc *curarg;
  97. found = 1;
  98. for (curarg = cur->args; curarg->name; curarg++)
  99. {
  100. int spacing = 20;
  101. spacing -= grub_strlen (curarg->name);
  102. grub_printf ("%s", curarg->name);
  103. switch (curarg->type)
  104. {
  105. case GRUB_PARTTOOL_ARG_BOOL:
  106. grub_printf ("+/-");
  107. spacing -= 3;
  108. break;
  109. case GRUB_PARTTOOL_ARG_VAL:
  110. grub_xputs (_("=VAL"));
  111. spacing -= 4;
  112. break;
  113. case GRUB_PARTTOOL_ARG_END:
  114. break;
  115. }
  116. while (spacing-- > 0)
  117. grub_printf (" ");
  118. grub_puts_ (curarg->desc);
  119. }
  120. }
  121. if (! found)
  122. grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
  123. dev->disk->partition->partmap->name);
  124. return GRUB_ERR_NONE;
  125. }
  126. static grub_err_t
  127. grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
  128. int argc, char **args)
  129. {
  130. grub_device_t dev;
  131. struct grub_parttool *cur, *ptool;
  132. int *parsed;
  133. int i, j;
  134. grub_err_t err = GRUB_ERR_NONE;
  135. if (argc < 1)
  136. {
  137. grub_puts_ (helpmsg);
  138. return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
  139. }
  140. if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
  141. {
  142. args[0][grub_strlen (args[0]) - 1] = 0;
  143. dev = grub_device_open (args[0] + 1);
  144. args[0][grub_strlen (args[0]) - 1] = ')';
  145. }
  146. else
  147. dev = grub_device_open (args[0]);
  148. if (! dev)
  149. return grub_errno;
  150. if (! dev->disk)
  151. {
  152. grub_device_close (dev);
  153. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
  154. }
  155. if (! dev->disk->partition)
  156. {
  157. grub_device_close (dev);
  158. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
  159. }
  160. /* Load modules. */
  161. if (! grub_no_modules)
  162. {
  163. const char *prefix;
  164. prefix = grub_env_get ("prefix");
  165. if (prefix)
  166. {
  167. char *filename;
  168. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
  169. "/parttool.lst", prefix);
  170. if (filename)
  171. {
  172. grub_file_t file;
  173. file = grub_file_open (filename);
  174. if (file)
  175. {
  176. char *buf = 0;
  177. for (;; grub_free(buf))
  178. {
  179. char *p, *name;
  180. buf = grub_file_getline (file);
  181. if (! buf)
  182. break;
  183. name = buf;
  184. while (grub_isspace (name[0]))
  185. name++;
  186. if (! grub_isgraph (name[0]))
  187. continue;
  188. p = grub_strchr (name, ':');
  189. if (! p)
  190. continue;
  191. *p = '\0';
  192. p++;
  193. while (*p == ' ' || *p == '\t')
  194. p++;
  195. if (! grub_isgraph (*p))
  196. continue;
  197. if (grub_strcmp (name, dev->disk->partition->partmap->name)
  198. != 0)
  199. continue;
  200. grub_dl_load (p);
  201. }
  202. grub_file_close (file);
  203. }
  204. grub_free (filename);
  205. }
  206. }
  207. /* Ignore errors. */
  208. grub_errno = GRUB_ERR_NONE;
  209. }
  210. if (argc == 1)
  211. {
  212. err = show_help (dev);
  213. grub_device_close (dev);
  214. return err;
  215. }
  216. for (i = 1; i < argc; i++)
  217. if (grub_strcmp (args[i], "help") == 0)
  218. {
  219. err = show_help (dev);
  220. grub_device_close (dev);
  221. return err;
  222. }
  223. parsed = (int *) grub_zalloc (argc * sizeof (int));
  224. for (i = 1; i < argc; i++)
  225. if (! parsed[i])
  226. {
  227. struct grub_parttool_argdesc *curarg;
  228. struct grub_parttool_args *pargs;
  229. for (cur = parts; cur; cur = cur->next)
  230. if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
  231. {
  232. for (curarg = cur->args; curarg->name; curarg++)
  233. if (grub_strncmp (curarg->name, args[i],
  234. grub_strlen (curarg->name)) == 0
  235. && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
  236. && (args[i][grub_strlen (curarg->name)] == '+'
  237. || args[i][grub_strlen (curarg->name)] == '-'
  238. || args[i][grub_strlen (curarg->name)] == 0))
  239. || (curarg->type == GRUB_PARTTOOL_ARG_VAL
  240. && args[i][grub_strlen (curarg->name)] == '=')))
  241. break;
  242. if (curarg->name)
  243. break;
  244. }
  245. if (! cur)
  246. {
  247. grub_free (parsed);
  248. grub_device_close (dev);
  249. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"),
  250. args[i]);
  251. }
  252. ptool = cur;
  253. pargs = (struct grub_parttool_args *)
  254. grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
  255. for (j = i; j < argc; j++)
  256. if (! parsed[j])
  257. {
  258. for (curarg = ptool->args; curarg->name; curarg++)
  259. if (grub_strncmp (curarg->name, args[j],
  260. grub_strlen (curarg->name)) == 0
  261. && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
  262. && (args[j][grub_strlen (curarg->name)] == '+'
  263. || args[j][grub_strlen (curarg->name)] == '-'
  264. || args[j][grub_strlen (curarg->name)] == 0))
  265. || (curarg->type == GRUB_PARTTOOL_ARG_VAL
  266. && args[j][grub_strlen (curarg->name)] == '=')))
  267. {
  268. parsed[j] = 1;
  269. pargs[curarg - ptool->args].set = 1;
  270. switch (curarg->type)
  271. {
  272. case GRUB_PARTTOOL_ARG_BOOL:
  273. pargs[curarg - ptool->args].bool
  274. = (args[j][grub_strlen (curarg->name)] != '-');
  275. break;
  276. case GRUB_PARTTOOL_ARG_VAL:
  277. pargs[curarg - ptool->args].str
  278. = (args[j] + grub_strlen (curarg->name) + 1);
  279. break;
  280. case GRUB_PARTTOOL_ARG_END:
  281. break;
  282. }
  283. }
  284. }
  285. err = ptool->func (dev, pargs);
  286. grub_free (pargs);
  287. if (err)
  288. break;
  289. }
  290. grub_free (parsed);
  291. grub_device_close (dev);
  292. return err;
  293. }
  294. static grub_command_t cmd;
  295. GRUB_MOD_INIT(parttool)
  296. {
  297. mymod = mod;
  298. cmd = grub_register_command ("parttool", grub_cmd_parttool,
  299. N_("PARTITION COMMANDS"),
  300. helpmsg);
  301. }
  302. GRUB_MOD_FINI(parttool)
  303. {
  304. grub_unregister_command (cmd);
  305. }