cmd_bmode.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Boundary Devices Inc.
  4. */
  5. #include <common.h>
  6. #include <linux/errno.h>
  7. #include <asm/io.h>
  8. #include <asm/mach-imx/boot_mode.h>
  9. #include <malloc.h>
  10. #include <command.h>
  11. static const struct boot_mode *modes[2];
  12. static const struct boot_mode *search_modes(char *arg)
  13. {
  14. int i;
  15. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  16. const struct boot_mode *p = modes[i];
  17. if (p) {
  18. while (p->name) {
  19. if (!strcmp(p->name, arg))
  20. return p;
  21. p++;
  22. }
  23. }
  24. }
  25. return NULL;
  26. }
  27. static int create_usage(char *dest)
  28. {
  29. int i;
  30. int size = 0;
  31. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  32. const struct boot_mode *p = modes[i];
  33. if (p) {
  34. while (p->name) {
  35. int len = strlen(p->name);
  36. if (dest) {
  37. memcpy(dest, p->name, len);
  38. dest += len;
  39. *dest++ = '|';
  40. }
  41. size += len + 1;
  42. p++;
  43. }
  44. }
  45. }
  46. if (dest)
  47. memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
  48. size += 10;
  49. return size;
  50. }
  51. static int do_boot_mode(struct cmd_tbl *cmdtp, int flag, int argc,
  52. char *const argv[])
  53. {
  54. const struct boot_mode *p;
  55. int reset_requested = 1;
  56. if (argc < 2)
  57. return CMD_RET_USAGE;
  58. p = search_modes(argv[1]);
  59. if (!p)
  60. return CMD_RET_USAGE;
  61. if (argc == 3) {
  62. if (strcmp(argv[2], "noreset"))
  63. return CMD_RET_USAGE;
  64. reset_requested = 0;
  65. }
  66. boot_mode_apply(p->cfg_val);
  67. if (reset_requested && p->cfg_val)
  68. do_reset(NULL, 0, 0, NULL);
  69. return 0;
  70. }
  71. U_BOOT_CMD(
  72. bmode, 3, 0, do_boot_mode,
  73. NULL,
  74. "");
  75. void add_board_boot_modes(const struct boot_mode *p)
  76. {
  77. int size;
  78. char *dest;
  79. struct cmd_tbl *entry = ll_entry_get(struct cmd_tbl, bmode, cmd);
  80. if (entry->usage) {
  81. free(entry->usage);
  82. entry->usage = NULL;
  83. }
  84. modes[0] = p;
  85. modes[1] = soc_boot_modes;
  86. size = create_usage(NULL);
  87. dest = malloc(size);
  88. if (dest) {
  89. create_usage(dest);
  90. entry->usage = dest;
  91. }
  92. }