parttool.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. "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. grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
  90. int argc, char **args)
  91. {
  92. grub_device_t dev;
  93. struct grub_parttool *cur, *ptool;
  94. int *parsed;
  95. int i, j;
  96. grub_err_t err = GRUB_ERR_NONE;
  97. auto grub_err_t show_help (void);
  98. grub_err_t show_help (void)
  99. {
  100. int found = 0;
  101. for (cur = parts; cur; cur = cur->next)
  102. if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
  103. {
  104. struct grub_parttool_argdesc *curarg;
  105. found = 1;
  106. for (curarg = cur->args; curarg->name; curarg++)
  107. {
  108. int spacing = 20;
  109. spacing -= grub_strlen (curarg->name);
  110. grub_printf ("%s", curarg->name);
  111. switch (curarg->type)
  112. {
  113. case GRUB_PARTTOOL_ARG_BOOL:
  114. grub_printf ("+/-");
  115. spacing -= 3;
  116. break;
  117. case GRUB_PARTTOOL_ARG_VAL:
  118. grub_printf ("=VAL");
  119. spacing -= 4;
  120. break;
  121. case GRUB_PARTTOOL_ARG_END:
  122. break;
  123. }
  124. while (spacing-- > 0)
  125. grub_printf (" ");
  126. grub_printf ("%s\n", curarg->desc);
  127. }
  128. }
  129. if (! found)
  130. grub_printf ("Sorry no parttool is available for %s\n",
  131. dev->disk->partition->partmap->name);
  132. return GRUB_ERR_NONE;
  133. }
  134. if (argc < 1)
  135. {
  136. grub_printf ("%s\n", helpmsg);
  137. return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
  138. }
  139. if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
  140. {
  141. args[0][grub_strlen (args[0]) - 1] = 0;
  142. dev = grub_device_open (args[0] + 1);
  143. args[0][grub_strlen (args[0]) - 1] = ')';
  144. }
  145. else
  146. dev = grub_device_open (args[0]);
  147. if (! dev)
  148. return grub_errno;
  149. if (! dev->disk)
  150. {
  151. grub_device_close (dev);
  152. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
  153. }
  154. if (! dev->disk->partition)
  155. {
  156. grub_device_close (dev);
  157. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
  158. }
  159. /* Load modules. */
  160. if (! grub_no_autoload)
  161. {
  162. const char *prefix;
  163. prefix = grub_env_get ("prefix");
  164. if (prefix)
  165. {
  166. char *filename;
  167. filename = grub_xasprintf ("%s/parttool.lst", prefix);
  168. if (filename)
  169. {
  170. grub_file_t file;
  171. file = grub_file_open (filename);
  172. if (file)
  173. {
  174. char *buf = 0;
  175. for (;; grub_free(buf))
  176. {
  177. char *p, *name;
  178. buf = grub_file_getline (file);
  179. if (! buf)
  180. break;
  181. name = buf;
  182. if (! grub_isgraph (name[0]))
  183. continue;
  184. p = grub_strchr (name, ':');
  185. if (! p)
  186. continue;
  187. *p = '\0';
  188. while (*++p == ' ')
  189. ;
  190. if (! grub_isgraph (*p))
  191. continue;
  192. if (grub_strcmp (name, dev->disk->partition->partmap->name)
  193. != 0)
  194. continue;
  195. grub_dl_load (p);
  196. }
  197. grub_file_close (file);
  198. }
  199. grub_free (filename);
  200. }
  201. }
  202. /* Ignore errors. */
  203. grub_errno = GRUB_ERR_NONE;
  204. }
  205. if (argc == 1)
  206. return show_help ();
  207. for (i = 1; i < argc; i++)
  208. if (grub_strcmp (args[i], "help") == 0)
  209. return show_help ();
  210. parsed = (int *) grub_zalloc (argc * sizeof (int));
  211. for (i = 1; i < argc; i++)
  212. if (! parsed[i])
  213. {
  214. struct grub_parttool_argdesc *curarg;
  215. struct grub_parttool_args *pargs;
  216. for (cur = parts; cur; cur = cur->next)
  217. if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
  218. {
  219. for (curarg = cur->args; curarg->name; curarg++)
  220. if (grub_strncmp (curarg->name, args[i],
  221. grub_strlen (curarg->name)) == 0
  222. && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
  223. && (args[i][grub_strlen (curarg->name)] == '+'
  224. || args[i][grub_strlen (curarg->name)] == '-'
  225. || args[i][grub_strlen (curarg->name)] == 0))
  226. || (curarg->type == GRUB_PARTTOOL_ARG_VAL
  227. && args[i][grub_strlen (curarg->name)] == '=')))
  228. break;
  229. if (curarg->name)
  230. break;
  231. }
  232. if (! cur)
  233. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
  234. args[i]);
  235. ptool = cur;
  236. pargs = (struct grub_parttool_args *)
  237. grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
  238. for (j = i; j < argc; j++)
  239. if (! parsed[j])
  240. {
  241. for (curarg = ptool->args; curarg->name; curarg++)
  242. if (grub_strncmp (curarg->name, args[j],
  243. grub_strlen (curarg->name)) == 0
  244. && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
  245. && (args[j][grub_strlen (curarg->name)] == '+'
  246. || args[j][grub_strlen (curarg->name)] == '-'
  247. || args[j][grub_strlen (curarg->name)] == 0))
  248. || (curarg->type == GRUB_PARTTOOL_ARG_VAL
  249. && args[j][grub_strlen (curarg->name)] == '=')))
  250. {
  251. parsed[j] = 1;
  252. pargs[curarg - ptool->args].set = 1;
  253. switch (curarg->type)
  254. {
  255. case GRUB_PARTTOOL_ARG_BOOL:
  256. pargs[curarg - ptool->args].bool
  257. = (args[j][grub_strlen (curarg->name)] != '-');
  258. break;
  259. case GRUB_PARTTOOL_ARG_VAL:
  260. pargs[curarg - ptool->args].str
  261. = (args[j] + grub_strlen (curarg->name) + 1);
  262. break;
  263. case GRUB_PARTTOOL_ARG_END:
  264. break;
  265. }
  266. }
  267. }
  268. err = ptool->func (dev, pargs);
  269. grub_free (pargs);
  270. if (err)
  271. break;
  272. }
  273. grub_free (parsed);
  274. grub_device_close (dev);
  275. return err;
  276. }
  277. static grub_command_t cmd;
  278. GRUB_MOD_INIT(parttool)
  279. {
  280. mymod = mod;
  281. cmd = grub_register_command ("parttool", grub_cmd_parttool,
  282. N_("PARTITION COMMANDS"),
  283. helpmsg);
  284. }
  285. GRUB_MOD_FINI(parttool)
  286. {
  287. grub_unregister_command (cmd);
  288. }