You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <getopt.h>
  29. #include <byteswap.h>
  30. #include <ipxe/settings.h>
  31. #include <ipxe/command.h>
  32. #include <ipxe/parseopt.h>
  33. #include <readline/readline.h>
  34. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  35. /** @file
  36. *
  37. * Non-volatile option commands
  38. *
  39. */
  40. /** "show" options */
  41. struct show_options {};
  42. /** "show" option list */
  43. static struct option_descriptor show_opts[] = {};
  44. /** "show" command descriptor */
  45. static struct command_descriptor show_cmd =
  46. COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
  47. /**
  48. * "show" command
  49. *
  50. * @v argc Argument count
  51. * @v argv Argument list
  52. * @ret rc Return status code
  53. */
  54. static int show_exec ( int argc, char **argv ) {
  55. struct show_options opts;
  56. struct named_setting setting;
  57. struct settings *origin;
  58. struct setting fetched;
  59. char name_buf[32];
  60. char *value;
  61. int len;
  62. int rc;
  63. /* Parse options */
  64. if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
  65. goto err_parse_options;
  66. /* Parse setting name */
  67. if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
  68. goto err_parse_setting;
  69. /* Fetch formatted setting value */
  70. if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
  71. &origin, &fetched, &value ) ) < 0 ) {
  72. rc = len;
  73. printf ( "Could not find \"%s\": %s\n",
  74. setting.setting.name, strerror ( rc ) );
  75. goto err_fetchf;
  76. }
  77. /* Print setting value */
  78. setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
  79. printf ( "%s = %s\n", name_buf, value );
  80. /* Success */
  81. rc = 0;
  82. free ( value );
  83. err_fetchf:
  84. err_parse_setting:
  85. err_parse_options:
  86. return rc;
  87. }
  88. /** "set", "clear", and "read" options */
  89. struct set_core_options {};
  90. /** "set", "clear", and "read" option list */
  91. static struct option_descriptor set_core_opts[] = {};
  92. /** "set" command descriptor */
  93. static struct command_descriptor set_cmd =
  94. COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
  95. "<setting> <value>" );
  96. /** "clear" and "read" command descriptor */
  97. static struct command_descriptor clear_read_cmd =
  98. COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
  99. "<setting>" );
  100. /**
  101. * "set", "clear", and "read" command
  102. *
  103. * @v argc Argument count
  104. * @v argv Argument list
  105. * @v cmd Command descriptor
  106. * @v get_value Method to obtain setting value
  107. * @ret rc Return status code
  108. */
  109. static int set_core_exec ( int argc, char **argv,
  110. struct command_descriptor *cmd,
  111. int ( * get_value ) ( struct named_setting *setting,
  112. char **args, char **value ) ) {
  113. struct set_core_options opts;
  114. struct named_setting setting;
  115. char *value;
  116. int rc;
  117. /* Parse options */
  118. if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
  119. goto err_parse_options;
  120. /* Parse setting name */
  121. if ( ( rc = parse_autovivified_setting ( argv[optind],
  122. &setting ) ) != 0 )
  123. goto err_parse_setting;
  124. /* Parse setting value */
  125. if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
  126. goto err_get_value;
  127. /* Apply default type if necessary */
  128. if ( ! setting.setting.type )
  129. setting.setting.type = &setting_type_string;
  130. /* Store setting */
  131. if ( ( rc = storef_setting ( setting.settings, &setting.setting,
  132. value ) ) != 0 ) {
  133. printf ( "Could not store \"%s\": %s\n",
  134. setting.setting.name, strerror ( rc ) );
  135. goto err_store;
  136. }
  137. err_store:
  138. free ( value );
  139. err_get_value:
  140. err_parse_setting:
  141. err_parse_options:
  142. return rc;
  143. }
  144. /**
  145. * Get setting value for "set" command
  146. *
  147. * @v setting Named setting
  148. * @v args Remaining arguments
  149. * @ret value Setting value
  150. * @ret rc Return status code
  151. */
  152. static int set_value ( struct named_setting *setting __unused,
  153. char **args, char **value ) {
  154. *value = concat_args ( args );
  155. if ( ! *value )
  156. return -ENOMEM;
  157. return 0;
  158. }
  159. /**
  160. * "set" command
  161. *
  162. * @v argc Argument count
  163. * @v argv Argument list
  164. * @ret rc Return status code
  165. */
  166. static int set_exec ( int argc, char **argv ) {
  167. return set_core_exec ( argc, argv, &set_cmd, set_value );
  168. }
  169. /**
  170. * Get setting value for "clear" command
  171. *
  172. * @v setting Named setting
  173. * @v args Remaining arguments
  174. * @ret value Setting value
  175. * @ret rc Return status code
  176. */
  177. static int clear_value ( struct named_setting *setting __unused,
  178. char **args __unused, char **value ) {
  179. *value = NULL;
  180. return 0;
  181. }
  182. /**
  183. * "clear" command
  184. *
  185. * @v argc Argument count
  186. * @v argv Argument list
  187. * @ret rc Return status code
  188. */
  189. static int clear_exec ( int argc, char **argv ) {
  190. return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
  191. }
  192. /**
  193. * Get setting value for "read" command
  194. *
  195. * @v setting Named setting
  196. * @v args Remaining arguments
  197. * @ret value Setting value
  198. * @ret rc Return status code
  199. */
  200. static int read_value ( struct named_setting *setting, char **args __unused,
  201. char **value ) {
  202. char *existing;
  203. int rc;
  204. /* Read existing value, treating errors as equivalent to an
  205. * empty initial setting.
  206. */
  207. fetchf_setting_copy ( setting->settings, &setting->setting,
  208. NULL, &setting->setting, &existing );
  209. /* Read new value */
  210. if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
  211. goto err_readline;
  212. err_readline:
  213. free ( existing );
  214. return rc;
  215. }
  216. /**
  217. * "read" command
  218. *
  219. * @v argc Argument count
  220. * @v argv Argument list
  221. * @ret rc Return status code
  222. */
  223. static int read_exec ( int argc, char **argv ) {
  224. return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
  225. }
  226. /** "inc" options */
  227. struct inc_options {};
  228. /** "inc" option list */
  229. static struct option_descriptor inc_opts[] = {};
  230. /** "inc" command descriptor */
  231. static struct command_descriptor inc_cmd =
  232. COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
  233. "<setting> [<increment>]" );
  234. /**
  235. * "inc" command
  236. *
  237. * @v argc Argument count
  238. * @v argv Argument list
  239. * @ret rc Return status code
  240. */
  241. static int inc_exec ( int argc, char **argv ) {
  242. struct inc_options opts;
  243. struct named_setting setting;
  244. unsigned int increment = 1;
  245. unsigned long value;
  246. int rc;
  247. /* Parse options */
  248. if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
  249. goto err_parse_options;
  250. /* Parse setting name */
  251. if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
  252. goto err_parse_setting;
  253. /* Parse increment (if present) */
  254. if ( ( ( optind + 1 ) < argc ) &&
  255. ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
  256. goto err_parse_increment;
  257. /* Read existing value, treating errors as equivalent to a
  258. * zero-valued :int32 initial setting.
  259. */
  260. if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
  261. NULL, &setting.setting, &value ) ) != 0 ) {
  262. value = 0;
  263. if ( ! setting.setting.type )
  264. setting.setting.type = &setting_type_int32;
  265. }
  266. /* Increment value */
  267. value += increment;
  268. /* Store updated setting value */
  269. if ( ( rc = storen_setting ( setting.settings, &setting.setting,
  270. value ) ) != 0 ) {
  271. printf ( "Could not store \"%s\": %s\n",
  272. setting.setting.name, strerror ( rc ) );
  273. goto err_store;
  274. }
  275. err_store:
  276. err_parse_increment:
  277. err_parse_setting:
  278. err_parse_options:
  279. return rc;
  280. }
  281. /** Non-volatile option commands */
  282. struct command nvo_commands[] __command = {
  283. {
  284. .name = "show",
  285. .exec = show_exec,
  286. },
  287. {
  288. .name = "set",
  289. .exec = set_exec,
  290. },
  291. {
  292. .name = "clear",
  293. .exec = clear_exec,
  294. },
  295. {
  296. .name = "read",
  297. .exec = read_exec,
  298. },
  299. {
  300. .name = "inc",
  301. .exec = inc_exec,
  302. },
  303. };