Root/jzboot/src/spl_cmdset.c

1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>,
4 * Peter Zotov <whitequark@whitequark.org>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <string.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
24
25#include "shell.h"
26#include "app_config.h"
27#include "ingenic.h"
28
29static int spl_memtest(shell_context_t *ctx, int argc, char *argv[]);
30static int spl_gpio(shell_context_t *ctx, int argc, char *argv[]);
31static int spl_boot(shell_context_t *ctx, int argc, char *argv[]);
32
33const shell_command_t spl_cmdset[] = {
34    { "memtest", "SDRAM test", spl_memtest, "[BASE] <SIZE>" },
35    { "gpio", "Set GPIO #PIN to STATE 0 or 1", spl_gpio, "<PIN> <STATE>" },
36    { "boot", "Load stage2 USB bootloader", spl_boot, NULL },
37    { NULL, NULL, NULL, NULL }
38};
39
40static int spl_stage1_op(shell_context_t *ctx, uint32_t op, uint32_t pin, uint32_t base, uint32_t size) {
41    if(cfg_getenv("STAGE1_FILE") == NULL) {
42        printf("Variable STAGE1_FILE is not set\n");
43
44        return -1;
45    }
46
47    int ret = ingenic_stage1_debugop(shell_device(ctx), cfg_getenv("STAGE1_FILE"), op, pin, base, size);
48
49    if(ret == -1)
50        perror("ingenic_stage1_debugop");
51
52    return ret;
53}
54
55static int spl_memtest(shell_context_t *ctx, int argc, char *argv[]) {
56    uint32_t start, size;
57
58    if(argc == 3) {
59        start = strtoul(argv[1], NULL, 0);
60        size = strtoul(argv[2], NULL, 0);
61    } else {
62        start = SDRAM_BASE;
63        size = ingenic_sdram_size(shell_device(ctx));
64    }
65
66    if(cfg_getenv("STAGE1_FILE") == NULL) {
67        printf("Variable STAGE1_FILE is not set\n");
68
69        return -1;
70    }
71
72    uint32_t fail;
73
74    int ret = ingenic_memtest(shell_device(ctx), cfg_getenv("STAGE1_FILE"), start, size, &fail);
75
76    if(ret == -1) {
77        if(errno == EFAULT) {
78            printf("Memory test failed at address 0x%08X\n", fail);
79        } else {
80            perror("ingenic_memtest");
81        }
82
83    } else {
84        printf("Memory test passed\n");
85    }
86
87    return ret;
88}
89
90static int spl_gpio(shell_context_t *ctx, int argc, char *argv[]) {
91    if(strcmp(argv[2], "0") && strcmp(argv[2], "1")) {
92        printf("Usage: %s <PIN> <STATE>\n", argv[0]);
93        printf(" STATE := 0 | 1\n");
94
95        return -1;
96    }
97
98    return spl_stage1_op(ctx, !strcmp(argv[2], "1") ? STAGE1_DEBUG_GPIO_SET : STAGE1_DEBUG_GPIO_CLEAR, atoi(argv[1]), 0, 0);
99}
100
101static int spl_boot(shell_context_t *ctx, int argc, char *argv[]) {
102    int ret = spl_stage1_op(ctx, STAGE1_DEBUG_BOOT, 0, 0, 0);
103
104    if(ret == -1)
105        return -1;
106
107    if(cfg_getenv("STAGE2_FILE") == NULL) {
108        printf("Variable STAGE2_FILE is not set\n");
109
110        return -1;
111    }
112
113    ret = ingenic_loadstage(shell_device(ctx), INGENIC_STAGE2, cfg_getenv("STAGE2_FILE"));
114
115    if(ret == -1) {
116        perror("ingenic_loadstage");
117
118        return -1;
119    }
120
121    ret = ingenic_configure_stage2(shell_device(ctx));
122
123    if(ret == -1)
124        perror("ingenic_configure_stage2");
125
126    return ret;
127}
128
129

Archive Download this file



interactive