cmd_bmode.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2012 Boundary Devices Inc.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/errno.h>
  24. #include <asm/io.h>
  25. #include <asm/imx-common/boot_mode.h>
  26. #include <malloc.h>
  27. #include <command.h>
  28. static const struct boot_mode *modes[2];
  29. static const struct boot_mode *search_modes(char *arg)
  30. {
  31. int i;
  32. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  33. const struct boot_mode *p = modes[i];
  34. if (p) {
  35. while (p->name) {
  36. if (!strcmp(p->name, arg))
  37. return p;
  38. p++;
  39. }
  40. }
  41. }
  42. return NULL;
  43. }
  44. static int create_usage(char *dest)
  45. {
  46. int i;
  47. int size = 0;
  48. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  49. const struct boot_mode *p = modes[i];
  50. if (p) {
  51. while (p->name) {
  52. int len = strlen(p->name);
  53. if (dest) {
  54. memcpy(dest, p->name, len);
  55. dest += len;
  56. *dest++ = '|';
  57. }
  58. size += len + 1;
  59. p++;
  60. }
  61. }
  62. }
  63. if (dest)
  64. memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
  65. size += 10;
  66. return size;
  67. }
  68. static int do_boot_mode(cmd_tbl_t *cmdtp, int flag, int argc,
  69. char * const argv[])
  70. {
  71. const struct boot_mode *p;
  72. int reset_requested = 1;
  73. if (argc < 2)
  74. return CMD_RET_USAGE;
  75. p = search_modes(argv[1]);
  76. if (!p)
  77. return CMD_RET_USAGE;
  78. if (argc == 3) {
  79. if (strcmp(argv[2], "noreset"))
  80. return CMD_RET_USAGE;
  81. reset_requested = 0;
  82. }
  83. boot_mode_apply(p->cfg_val);
  84. if (reset_requested && p->cfg_val)
  85. do_reset(NULL, 0, 0, NULL);
  86. return 0;
  87. }
  88. U_BOOT_CMD(
  89. bmode, 3, 0, do_boot_mode,
  90. NULL,
  91. "");
  92. void add_board_boot_modes(const struct boot_mode *p)
  93. {
  94. int size;
  95. char *dest;
  96. cmd_tbl_t *entry = ll_entry_get(cmd_tbl_t, bmode, cmd);
  97. if (entry->usage) {
  98. free(entry->usage);
  99. entry->usage = NULL;
  100. }
  101. modes[0] = p;
  102. modes[1] = soc_boot_modes;
  103. size = create_usage(NULL);
  104. dest = malloc(size);
  105. if (dest) {
  106. create_usage(dest);
  107. entry->usage = dest;
  108. }
  109. }