bootflow.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_BOOTSTD
  7. #include <common.h>
  8. #include <bootdev.h>
  9. #include <bootflow.h>
  10. #include <bootmeth.h>
  11. #include <bootstd.h>
  12. #include <dm.h>
  13. #include <env_internal.h>
  14. #include <malloc.h>
  15. #include <serial.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/uclass-internal.h>
  18. /* error codes used to signal running out of things */
  19. enum {
  20. BF_NO_MORE_PARTS = -ESHUTDOWN,
  21. BF_NO_MORE_DEVICES = -ENODEV,
  22. };
  23. /**
  24. * bootflow_state - name for each state
  25. *
  26. * See enum bootflow_state_t for what each of these means
  27. */
  28. static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
  29. "base",
  30. "media",
  31. "part",
  32. "fs",
  33. "file",
  34. "ready",
  35. };
  36. const char *bootflow_state_get_name(enum bootflow_state_t state)
  37. {
  38. /* This doesn't need to be a useful name, since it will never occur */
  39. if (state < 0 || state >= BOOTFLOWST_COUNT)
  40. return "?";
  41. return bootflow_state[state];
  42. }
  43. int bootflow_first_glob(struct bootflow **bflowp)
  44. {
  45. struct bootstd_priv *std;
  46. int ret;
  47. ret = bootstd_get_priv(&std);
  48. if (ret)
  49. return ret;
  50. if (list_empty(&std->glob_head))
  51. return -ENOENT;
  52. *bflowp = list_first_entry(&std->glob_head, struct bootflow,
  53. glob_node);
  54. return 0;
  55. }
  56. int bootflow_next_glob(struct bootflow **bflowp)
  57. {
  58. struct bootstd_priv *std;
  59. struct bootflow *bflow = *bflowp;
  60. int ret;
  61. ret = bootstd_get_priv(&std);
  62. if (ret)
  63. return ret;
  64. *bflowp = NULL;
  65. if (list_is_last(&bflow->glob_node, &std->glob_head))
  66. return -ENOENT;
  67. *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
  68. return 0;
  69. }
  70. void bootflow_iter_init(struct bootflow_iter *iter, int flags)
  71. {
  72. memset(iter, '\0', sizeof(*iter));
  73. iter->first_glob_method = -1;
  74. iter->flags = flags;
  75. /* remember the first bootdevs we see */
  76. iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
  77. }
  78. void bootflow_iter_uninit(struct bootflow_iter *iter)
  79. {
  80. free(iter->method_order);
  81. }
  82. int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
  83. const struct udevice *bmeth)
  84. {
  85. /* We only support disabling the current bootmeth */
  86. if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
  87. iter->method_order[iter->cur_method] != bmeth)
  88. return -EINVAL;
  89. memmove(&iter->method_order[iter->cur_method],
  90. &iter->method_order[iter->cur_method + 1],
  91. (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
  92. iter->num_methods--;
  93. return 0;
  94. }
  95. /**
  96. * bootflow_iter_set_dev() - switch to the next bootdev when iterating
  97. *
  98. * This sets iter->dev, records the device in the dev_used[] list and shows a
  99. * message if required
  100. *
  101. * @iter: Iterator to update
  102. * @dev: Bootdev to use, or NULL if there are no more
  103. */
  104. static void bootflow_iter_set_dev(struct bootflow_iter *iter,
  105. struct udevice *dev, int method_flags)
  106. {
  107. struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
  108. log_debug("iter: Setting dev to %s, flags %x\n",
  109. dev ? dev->name : "(none)", method_flags);
  110. iter->dev = dev;
  111. iter->method_flags = method_flags;
  112. if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
  113. /* record the device for later */
  114. if (dev && iter->num_devs < iter->max_devs)
  115. iter->dev_used[iter->num_devs++] = dev;
  116. if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
  117. BOOTFLOWIF_SHOW) {
  118. if (dev)
  119. printf("Scanning bootdev '%s':\n", dev->name);
  120. else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
  121. ucp->flags & BOOTMETHF_GLOBAL)
  122. printf("Scanning global bootmeth '%s':\n",
  123. iter->method->name);
  124. else
  125. printf("No more bootdevs\n");
  126. }
  127. }
  128. }
  129. /**
  130. * iter_incr() - Move to the next item (method, part, bootdev)
  131. *
  132. * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
  133. */
  134. static int iter_incr(struct bootflow_iter *iter)
  135. {
  136. struct udevice *dev;
  137. bool inc_dev = true;
  138. bool global;
  139. int ret;
  140. log_debug("entry: err=%d\n", iter->err);
  141. global = iter->doing_global;
  142. if (iter->err == BF_NO_MORE_DEVICES)
  143. return BF_NO_MORE_DEVICES;
  144. if (iter->err != BF_NO_MORE_PARTS) {
  145. /* Get the next boothmethod */
  146. if (++iter->cur_method < iter->num_methods) {
  147. iter->method = iter->method_order[iter->cur_method];
  148. return 0;
  149. }
  150. /*
  151. * If we have finished scanning the global bootmeths, start the
  152. * normal bootdev scan
  153. */
  154. if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
  155. iter->num_methods = iter->first_glob_method;
  156. iter->doing_global = false;
  157. /*
  158. * Don't move to the next dev as we haven't tried this
  159. * one yet!
  160. */
  161. inc_dev = false;
  162. }
  163. }
  164. /* No more bootmeths; start at the first one, and... */
  165. iter->cur_method = 0;
  166. iter->method = iter->method_order[iter->cur_method];
  167. if (iter->err != BF_NO_MORE_PARTS) {
  168. /* ...select next partition */
  169. if (++iter->part <= iter->max_part)
  170. return 0;
  171. }
  172. /* No more partitions; start at the first one and... */
  173. iter->part = 0;
  174. /*
  175. * Note: as far as we know, there is no partition table on the next
  176. * bootdev, so set max_part to 0 until we discover otherwise. See
  177. * bootdev_find_in_blk() for where this is set.
  178. */
  179. iter->max_part = 0;
  180. /* ...select next bootdev */
  181. if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
  182. ret = -ENOENT;
  183. } else {
  184. int method_flags;
  185. ret = 0;
  186. dev = iter->dev;
  187. log_debug("inc_dev=%d\n", inc_dev);
  188. if (!inc_dev) {
  189. ret = bootdev_setup_iter(iter, NULL, &dev,
  190. &method_flags);
  191. } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
  192. (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
  193. /* Move to the next bootdev in this uclass */
  194. uclass_find_next_device(&dev);
  195. if (!dev) {
  196. log_debug("finished uclass %s\n",
  197. dev_get_uclass_name(dev));
  198. ret = -ENODEV;
  199. }
  200. } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
  201. iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
  202. log_debug("next in single\n");
  203. method_flags = 0;
  204. do {
  205. /*
  206. * Move to the next bootdev child of this media
  207. * device. This ensures that we cover all the
  208. * available SCSI IDs and LUNs.
  209. */
  210. device_find_next_child(&dev);
  211. log_debug("- next %s\n",
  212. dev ? dev->name : "(none)");
  213. } while (dev && device_get_uclass_id(dev) !=
  214. UCLASS_BOOTDEV);
  215. if (!dev) {
  216. log_debug("finished uclass %s\n",
  217. dev_get_uclass_name(dev));
  218. ret = -ENODEV;
  219. }
  220. } else {
  221. log_debug("labels %p\n", iter->labels);
  222. if (iter->labels) {
  223. ret = bootdev_next_label(iter, &dev,
  224. &method_flags);
  225. } else {
  226. ret = bootdev_next_prio(iter, &dev);
  227. method_flags = 0;
  228. }
  229. }
  230. log_debug("ret=%d, dev=%p %s\n", ret, dev,
  231. dev ? dev->name : "none");
  232. if (ret) {
  233. bootflow_iter_set_dev(iter, NULL, 0);
  234. } else {
  235. /*
  236. * Probe the bootdev. This does not probe any attached
  237. * block device, since they are siblings
  238. */
  239. ret = device_probe(dev);
  240. log_debug("probe %s %d\n", dev->name, ret);
  241. if (!log_msg_ret("probe", ret))
  242. bootflow_iter_set_dev(iter, dev, method_flags);
  243. }
  244. }
  245. /* if there are no more bootdevs, give up */
  246. if (ret)
  247. return log_msg_ret("incr", BF_NO_MORE_DEVICES);
  248. return 0;
  249. }
  250. /**
  251. * bootflow_check() - Check if a bootflow can be obtained
  252. *
  253. * @iter: Provides part, bootmeth to use
  254. * @bflow: Bootflow to update on success
  255. * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
  256. * BF_NO_MORE_PARTS if there are no more partitions on bootdev
  257. */
  258. static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
  259. {
  260. struct udevice *dev;
  261. int ret;
  262. if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
  263. bootflow_iter_set_dev(iter, NULL, 0);
  264. ret = bootmeth_get_bootflow(iter->method, bflow);
  265. if (ret)
  266. return log_msg_ret("glob", ret);
  267. return 0;
  268. }
  269. dev = iter->dev;
  270. ret = bootdev_get_bootflow(dev, iter, bflow);
  271. /* If we got a valid bootflow, return it */
  272. if (!ret) {
  273. log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
  274. dev->name, iter->part, iter->method->name);
  275. return 0;
  276. }
  277. /* Unless there is nothing more to try, move to the next device */
  278. else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
  279. log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
  280. dev->name, iter->part, iter->method->name, ret);
  281. /*
  282. * For 'all' we return all bootflows, even
  283. * those with errors
  284. */
  285. if (iter->flags & BOOTFLOWIF_ALL)
  286. return log_msg_ret("all", ret);
  287. }
  288. if (ret)
  289. return log_msg_ret("check", ret);
  290. return 0;
  291. }
  292. int bootflow_scan_first(struct udevice *dev, const char *label,
  293. struct bootflow_iter *iter, int flags,
  294. struct bootflow *bflow)
  295. {
  296. int ret;
  297. if (dev || label)
  298. flags |= BOOTFLOWIF_SKIP_GLOBAL;
  299. bootflow_iter_init(iter, flags);
  300. /*
  301. * Set up the ordering of bootmeths. This sets iter->doing_global and
  302. * iter->first_glob_method if we are starting with the global bootmeths
  303. */
  304. ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
  305. if (ret)
  306. return log_msg_ret("obmeth", -ENODEV);
  307. /* Find the first bootmeth (there must be at least one!) */
  308. iter->method = iter->method_order[iter->cur_method];
  309. if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
  310. struct udevice *dev = NULL;
  311. int method_flags;
  312. ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
  313. if (ret)
  314. return log_msg_ret("obdev", -ENODEV);
  315. bootflow_iter_set_dev(iter, dev, method_flags);
  316. }
  317. ret = bootflow_check(iter, bflow);
  318. if (ret) {
  319. log_debug("check - ret=%d\n", ret);
  320. if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
  321. if (iter->flags & BOOTFLOWIF_ALL)
  322. return log_msg_ret("all", ret);
  323. }
  324. iter->err = ret;
  325. ret = bootflow_scan_next(iter, bflow);
  326. if (ret)
  327. return log_msg_ret("get", ret);
  328. }
  329. return 0;
  330. }
  331. int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
  332. {
  333. int ret;
  334. do {
  335. ret = iter_incr(iter);
  336. log_debug("iter_incr: ret=%d\n", ret);
  337. if (ret == BF_NO_MORE_DEVICES)
  338. return log_msg_ret("done", ret);
  339. if (!ret) {
  340. ret = bootflow_check(iter, bflow);
  341. log_debug("check - ret=%d\n", ret);
  342. if (!ret)
  343. return 0;
  344. iter->err = ret;
  345. if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
  346. if (iter->flags & BOOTFLOWIF_ALL)
  347. return log_msg_ret("all", ret);
  348. }
  349. } else {
  350. log_debug("incr failed, err=%d\n", ret);
  351. iter->err = ret;
  352. }
  353. } while (1);
  354. }
  355. void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
  356. struct udevice *meth)
  357. {
  358. memset(bflow, '\0', sizeof(*bflow));
  359. bflow->dev = bootdev;
  360. bflow->method = meth;
  361. bflow->state = BOOTFLOWST_BASE;
  362. }
  363. void bootflow_free(struct bootflow *bflow)
  364. {
  365. free(bflow->name);
  366. free(bflow->subdir);
  367. free(bflow->fname);
  368. free(bflow->buf);
  369. free(bflow->os_name);
  370. free(bflow->fdt_fname);
  371. }
  372. void bootflow_remove(struct bootflow *bflow)
  373. {
  374. if (bflow->dev)
  375. list_del(&bflow->bm_node);
  376. list_del(&bflow->glob_node);
  377. bootflow_free(bflow);
  378. free(bflow);
  379. }
  380. int bootflow_boot(struct bootflow *bflow)
  381. {
  382. int ret;
  383. if (bflow->state != BOOTFLOWST_READY)
  384. return log_msg_ret("load", -EPROTO);
  385. ret = bootmeth_boot(bflow->method, bflow);
  386. if (ret)
  387. return log_msg_ret("boot", ret);
  388. /*
  389. * internal error, should not get here since we should have booted
  390. * something or returned an error
  391. */
  392. return log_msg_ret("end", -EFAULT);
  393. }
  394. int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
  395. {
  396. int ret;
  397. printf("** Booting bootflow '%s' with %s\n", bflow->name,
  398. bflow->method->name);
  399. if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
  400. (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
  401. printf("Using prior-stage device tree\n");
  402. ret = bootflow_boot(bflow);
  403. if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
  404. printf("Boot failed (err=%d)\n", ret);
  405. return ret;
  406. }
  407. switch (ret) {
  408. case -EPROTO:
  409. printf("Bootflow not loaded (state '%s')\n",
  410. bootflow_state_get_name(bflow->state));
  411. break;
  412. case -ENOSYS:
  413. printf("Boot method '%s' not supported\n", bflow->method->name);
  414. break;
  415. case -ENOTSUPP:
  416. /* Disable this bootflow for this iteration */
  417. if (iter) {
  418. int ret2;
  419. ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
  420. if (!ret2) {
  421. printf("Boot method '%s' failed and will not be retried\n",
  422. bflow->method->name);
  423. }
  424. }
  425. break;
  426. default:
  427. printf("Boot failed (err=%d)\n", ret);
  428. break;
  429. }
  430. return ret;
  431. }
  432. int bootflow_iter_check_blk(const struct bootflow_iter *iter)
  433. {
  434. const struct udevice *media = dev_get_parent(iter->dev);
  435. enum uclass_id id = device_get_uclass_id(media);
  436. log_debug("uclass %d: %s\n", id, uclass_get_name(id));
  437. if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
  438. return 0;
  439. return -ENOTSUPP;
  440. }
  441. int bootflow_iter_check_sf(const struct bootflow_iter *iter)
  442. {
  443. const struct udevice *media = dev_get_parent(iter->dev);
  444. enum uclass_id id = device_get_uclass_id(media);
  445. log_debug("uclass %d: %s\n", id, uclass_get_name(id));
  446. if (id == UCLASS_SPI_FLASH)
  447. return 0;
  448. return -ENOTSUPP;
  449. }
  450. int bootflow_iter_check_net(const struct bootflow_iter *iter)
  451. {
  452. const struct udevice *media = dev_get_parent(iter->dev);
  453. enum uclass_id id = device_get_uclass_id(media);
  454. log_debug("uclass %d: %s\n", id, uclass_get_name(id));
  455. if (id == UCLASS_ETH)
  456. return 0;
  457. return -ENOTSUPP;
  458. }
  459. int bootflow_iter_check_system(const struct bootflow_iter *iter)
  460. {
  461. const struct udevice *media = dev_get_parent(iter->dev);
  462. enum uclass_id id = device_get_uclass_id(media);
  463. log_debug("uclass %d: %s\n", id, uclass_get_name(id));
  464. if (id == UCLASS_BOOTSTD)
  465. return 0;
  466. return -ENOTSUPP;
  467. }
  468. /**
  469. * bootflow_cmdline_set() - Set the command line for a bootflow
  470. *
  471. * @value: New command-line string
  472. * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
  473. */
  474. int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
  475. {
  476. char *cmdline = NULL;
  477. if (value) {
  478. cmdline = strdup(value);
  479. if (!cmdline)
  480. return -ENOMEM;
  481. }
  482. free(bflow->cmdline);
  483. bflow->cmdline = cmdline;
  484. return 0;
  485. }
  486. #ifdef CONFIG_BOOTSTD_FULL
  487. /**
  488. * on_bootargs() - Update the cmdline of a bootflow
  489. */
  490. static int on_bootargs(const char *name, const char *value, enum env_op op,
  491. int flags)
  492. {
  493. struct bootstd_priv *std;
  494. struct bootflow *bflow;
  495. int ret;
  496. ret = bootstd_get_priv(&std);
  497. if (ret)
  498. return 0;
  499. bflow = std->cur_bootflow;
  500. if (!bflow)
  501. return 0;
  502. switch (op) {
  503. case env_op_create:
  504. case env_op_overwrite:
  505. ret = bootflow_cmdline_set(bflow, value);
  506. if (ret && ret != ENOENT)
  507. return 1;
  508. return 0;
  509. case env_op_delete:
  510. bootflow_cmdline_set(bflow, NULL);
  511. fallthrough;
  512. default:
  513. return 0;
  514. }
  515. }
  516. U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
  517. #endif
  518. /**
  519. * copy_in() - Copy a string into a cmdline buffer
  520. *
  521. * @buf: Buffer to copy into
  522. * @end: End of buffer (pointer to char after the end)
  523. * @arg: String to copy from
  524. * @len: Number of chars to copy from @arg (note that this is not usually the
  525. * sane as strlen(arg) since the string may contain following arguments)
  526. * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
  527. * with no '=' sign
  528. * Returns: Number of chars written to @buf
  529. */
  530. static int copy_in(char *buf, char *end, const char *arg, int len,
  531. const char *new_val)
  532. {
  533. char *to = buf;
  534. /* copy the arg name */
  535. if (to + len >= end)
  536. return -E2BIG;
  537. memcpy(to, arg, len);
  538. to += len;
  539. if (new_val == BOOTFLOWCL_EMPTY) {
  540. /* no value */
  541. } else {
  542. bool need_quote = strchr(new_val, ' ');
  543. len = strlen(new_val);
  544. /* need space for value, equals sign and maybe two quotes */
  545. if (to + 1 + (need_quote ? 2 : 0) + len >= end)
  546. return -E2BIG;
  547. *to++ = '=';
  548. if (need_quote)
  549. *to++ = '"';
  550. memcpy(to, new_val, len);
  551. to += len;
  552. if (need_quote)
  553. *to++ = '"';
  554. }
  555. return to - buf;
  556. }
  557. int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
  558. const char *set_arg, const char *new_val, int *posp)
  559. {
  560. bool found_arg = false;
  561. const char *from;
  562. char *to, *end;
  563. int set_arg_len;
  564. char empty = '\0';
  565. int ret;
  566. from = cmdline ?: &empty;
  567. /* check if the value has quotes inside */
  568. if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
  569. return -EBADF;
  570. set_arg_len = strlen(set_arg);
  571. for (to = buf, end = buf + maxlen; *from;) {
  572. const char *val, *arg_end, *val_end, *p;
  573. bool in_quote;
  574. if (to >= end)
  575. return -E2BIG;
  576. while (*from == ' ')
  577. from++;
  578. if (!*from)
  579. break;
  580. /* find the end of this arg */
  581. val = NULL;
  582. arg_end = NULL;
  583. val_end = NULL;
  584. in_quote = false;
  585. for (p = from;; p++) {
  586. if (in_quote) {
  587. if (!*p)
  588. return -EINVAL;
  589. if (*p == '"')
  590. in_quote = false;
  591. continue;
  592. }
  593. if (*p == '=') {
  594. arg_end = p;
  595. val = p + 1;
  596. } else if (*p == '"') {
  597. in_quote = true;
  598. } else if (!*p || *p == ' ') {
  599. val_end = p;
  600. if (!arg_end)
  601. arg_end = p;
  602. break;
  603. }
  604. }
  605. /*
  606. * At this point val_end points to the end of the value, or the
  607. * last char after the arg name, if there is no label.
  608. * arg_end is the char after the arg name
  609. * val points to the value, or NULL if there is none
  610. * char after the value.
  611. *
  612. * fred=1234
  613. * ^ ^^ ^
  614. * from || |
  615. * / \ \
  616. * arg_end val val_end
  617. */
  618. log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
  619. (long)(arg_end - from), (long)(val - from),
  620. (long)(val_end - from));
  621. if (to != buf) {
  622. if (to >= end)
  623. return -E2BIG;
  624. *to++ = ' ';
  625. }
  626. /* if this is the target arg, update it */
  627. if (!strncmp(from, set_arg, arg_end - from)) {
  628. if (!buf) {
  629. bool has_quote = val_end[-1] == '"';
  630. /*
  631. * exclude any start/end quotes from
  632. * calculations
  633. */
  634. if (!val)
  635. val = val_end;
  636. *posp = val - cmdline + has_quote;
  637. return val_end - val - 2 * has_quote;
  638. }
  639. found_arg = true;
  640. if (!new_val) {
  641. /* delete this arg */
  642. from = val_end + (*val_end == ' ');
  643. log_debug("delete from: %s\n", from);
  644. if (to != buf)
  645. to--; /* drop the space we added */
  646. continue;
  647. }
  648. ret = copy_in(to, end, from, arg_end - from, new_val);
  649. if (ret < 0)
  650. return ret;
  651. to += ret;
  652. /* if not the target arg, copy it unchanged */
  653. } else if (to) {
  654. int len;
  655. len = val_end - from;
  656. if (to + len >= end)
  657. return -E2BIG;
  658. memcpy(to, from, len);
  659. to += len;
  660. }
  661. from = val_end;
  662. }
  663. /* If we didn't find the arg, add it */
  664. if (!found_arg) {
  665. /* trying to delete something that is not there */
  666. if (!new_val || !buf)
  667. return -ENOENT;
  668. if (to >= end)
  669. return -E2BIG;
  670. /* add a space to separate it from the previous arg */
  671. if (to != buf && to[-1] != ' ')
  672. *to++ = ' ';
  673. ret = copy_in(to, end, set_arg, set_arg_len, new_val);
  674. log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
  675. if (ret < 0)
  676. return ret;
  677. to += ret;
  678. }
  679. /* delete any trailing space */
  680. if (to > buf && to[-1] == ' ')
  681. to--;
  682. if (to >= end)
  683. return -E2BIG;
  684. *to++ = '\0';
  685. return to - buf;
  686. }
  687. int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
  688. const char *new_val, bool set_env)
  689. {
  690. char buf[2048];
  691. char *cmd = NULL;
  692. int ret;
  693. ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
  694. new_val, NULL);
  695. if (ret < 0)
  696. return ret;
  697. ret = bootflow_cmdline_set(bflow, buf);
  698. if (*buf) {
  699. cmd = strdup(buf);
  700. if (!cmd)
  701. return -ENOMEM;
  702. }
  703. free(bflow->cmdline);
  704. bflow->cmdline = cmd;
  705. if (set_env) {
  706. ret = env_set("bootargs", bflow->cmdline);
  707. if (ret)
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
  713. {
  714. int ret;
  715. ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
  716. return ret;
  717. }
  718. int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
  719. const char **val)
  720. {
  721. int ret;
  722. int pos;
  723. ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
  724. if (ret < 0)
  725. return ret;
  726. *val = bflow->cmdline + pos;
  727. return ret;
  728. }
  729. int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
  730. {
  731. struct serial_device_info info;
  732. char buf[50];
  733. int ret;
  734. ret = serial_getinfo(gd->cur_serial_dev, &info);
  735. if (ret)
  736. return ret;
  737. *buf = '\0';
  738. if (!strcmp("earlycon", arg)) {
  739. snprintf(buf, sizeof(buf),
  740. "uart8250,mmio32,%#lx,%dn8", info.addr,
  741. info.baudrate);
  742. } else if (!strcmp("console", arg)) {
  743. snprintf(buf, sizeof(buf),
  744. "ttyS0,%dn8", info.baudrate);
  745. }
  746. if (!*buf) {
  747. printf("Unknown param '%s\n", arg);
  748. return -ENOENT;
  749. }
  750. ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
  751. if (ret)
  752. return ret;
  753. return 0;
  754. }