cmd_bmode.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. static const struct boot_mode *modes[2];
  28. static const struct boot_mode *search_modes(char *arg)
  29. {
  30. int i;
  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. if (!strcmp(p->name, arg))
  36. return p;
  37. p++;
  38. }
  39. }
  40. }
  41. return NULL;
  42. }
  43. static int create_usage(char *dest)
  44. {
  45. int i;
  46. int size = 0;
  47. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  48. const struct boot_mode *p = modes[i];
  49. if (p) {
  50. while (p->name) {
  51. int len = strlen(p->name);
  52. if (dest) {
  53. memcpy(dest, p->name, len);
  54. dest += len;
  55. *dest++ = '|';
  56. }
  57. size += len + 1;
  58. p++;
  59. }
  60. }
  61. }
  62. if (dest)
  63. memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
  64. size += 10;
  65. return size;
  66. }
  67. static int do_boot_mode(cmd_tbl_t *cmdtp, int flag, int argc,
  68. char * const argv[])
  69. {
  70. const struct boot_mode *p;
  71. int reset_requested = 1;
  72. if (argc < 2)
  73. return CMD_RET_USAGE;
  74. p = search_modes(argv[1]);
  75. if (!p)
  76. return CMD_RET_USAGE;
  77. if (argc == 3) {
  78. if (strcmp(argv[2], "noreset"))
  79. return CMD_RET_USAGE;
  80. reset_requested = 0;
  81. }
  82. boot_mode_apply(p->cfg_val);
  83. if (reset_requested && p->cfg_val)
  84. do_reset(NULL, 0, 0, NULL);
  85. return 0;
  86. }
  87. U_BOOT_CMD(
  88. bmode, 3, 0, do_boot_mode,
  89. NULL,
  90. "");
  91. void add_board_boot_modes(const struct boot_mode *p)
  92. {
  93. int size;
  94. char *dest;
  95. if (__u_boot_cmd_bmode.usage) {
  96. free(__u_boot_cmd_bmode.usage);
  97. __u_boot_cmd_bmode.usage = NULL;
  98. }
  99. modes[0] = p;
  100. modes[1] = soc_boot_modes;
  101. size = create_usage(NULL);
  102. dest = malloc(size);
  103. if (dest) {
  104. create_usage(dest);
  105. __u_boot_cmd_bmode.usage = dest;
  106. }
  107. }