Troy Kisky | 124a06d | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 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> |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame^] | 27 | #include <command.h> |
Troy Kisky | 124a06d | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 28 | |
| 29 | static const struct boot_mode *modes[2]; |
| 30 | |
| 31 | static const struct boot_mode *search_modes(char *arg) |
| 32 | { |
| 33 | int i; |
| 34 | |
| 35 | for (i = 0; i < ARRAY_SIZE(modes); i++) { |
| 36 | const struct boot_mode *p = modes[i]; |
| 37 | if (p) { |
| 38 | while (p->name) { |
| 39 | if (!strcmp(p->name, arg)) |
| 40 | return p; |
| 41 | p++; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | static int create_usage(char *dest) |
| 49 | { |
| 50 | int i; |
| 51 | int size = 0; |
| 52 | |
| 53 | for (i = 0; i < ARRAY_SIZE(modes); i++) { |
| 54 | const struct boot_mode *p = modes[i]; |
| 55 | if (p) { |
| 56 | while (p->name) { |
| 57 | int len = strlen(p->name); |
| 58 | if (dest) { |
| 59 | memcpy(dest, p->name, len); |
| 60 | dest += len; |
| 61 | *dest++ = '|'; |
| 62 | } |
| 63 | size += len + 1; |
| 64 | p++; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | if (dest) |
| 69 | memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */ |
| 70 | size += 10; |
| 71 | return size; |
| 72 | } |
| 73 | |
| 74 | static int do_boot_mode(cmd_tbl_t *cmdtp, int flag, int argc, |
| 75 | char * const argv[]) |
| 76 | { |
| 77 | const struct boot_mode *p; |
| 78 | int reset_requested = 1; |
| 79 | |
| 80 | if (argc < 2) |
| 81 | return CMD_RET_USAGE; |
| 82 | p = search_modes(argv[1]); |
| 83 | if (!p) |
| 84 | return CMD_RET_USAGE; |
| 85 | if (argc == 3) { |
| 86 | if (strcmp(argv[2], "noreset")) |
| 87 | return CMD_RET_USAGE; |
| 88 | reset_requested = 0; |
| 89 | } |
| 90 | |
| 91 | boot_mode_apply(p->cfg_val); |
| 92 | if (reset_requested && p->cfg_val) |
| 93 | do_reset(NULL, 0, 0, NULL); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | U_BOOT_CMD( |
| 98 | bmode, 3, 0, do_boot_mode, |
| 99 | NULL, |
| 100 | ""); |
| 101 | |
| 102 | void add_board_boot_modes(const struct boot_mode *p) |
| 103 | { |
| 104 | int size; |
| 105 | char *dest; |
| 106 | |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame^] | 107 | cmd_tbl_t *entry = ll_entry_get(cmd_tbl_t, bmode, cmd); |
| 108 | |
| 109 | if (entry->usage) { |
| 110 | free(entry->usage); |
| 111 | entry->usage = NULL; |
Troy Kisky | 124a06d | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | modes[0] = p; |
| 115 | modes[1] = soc_boot_modes; |
| 116 | size = create_usage(NULL); |
| 117 | dest = malloc(size); |
| 118 | if (dest) { |
| 119 | create_usage(dest); |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame^] | 120 | entry->usage = dest; |
Troy Kisky | 124a06d | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 121 | } |
| 122 | } |