Root/jzboot/src/usbboot_cmdset.c

1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Peter Zotov <whitequark@whitequark.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <string.h>
20#include <stdio.h>
21#include <stdlib.h>
22
23#include "shell.h"
24#include "app_config.h"
25#include "ingenic.h"
26#include "elfldr.h"
27
28static int usbboot_boot(shell_context_t *ctx, int argc, char *argv[]);
29static int usbboot_load(shell_context_t *ctx, int argc, char *argv[]);
30static int usbboot_go(shell_context_t *ctx, int argc, char *argv[]);
31static int usbboot_load_kernel(shell_context_t *ctx, int argc, char *argv[]);
32static int usbboot_nquery(shell_context_t *ctx, int argc, char *argv[]);
33static int usbboot_ndump(shell_context_t *ctx, int argc, char *argv[]);
34static int usbboot_nerase(shell_context_t *ctx, int argc, char *argv[]);
35static int usbboot_nprogram(shell_context_t *ctx, int argc, char *argv[]);
36static int usbboot_nload(shell_context_t *ctx, int argc, char *argv[]);
37
38const shell_command_t usbboot_cmdset[] = {
39
40    { "boot", "Reconfigure stage2", usbboot_boot, NULL },
41    { "load", "Load file to SDRAM", usbboot_load, "<FILE> <BASE>" },
42    { "go", "Jump to <ADDRESS>", usbboot_go, "<ADDRESS>" },
43    { "load_kernel", "Load ELF kernel and initrd to memory", usbboot_load_kernel, "<KERNEL> <CMDLINE> [INITRAMFS]" },
44
45    { "nquery", "Query NAND information", usbboot_nquery, "<DEVICE>" },
46    { "ndump", "Dump NAND to file", usbboot_ndump, "<DEVICE> <STARTPAGE> <PAGES> <FILE>" },
47    { "ndump_oob", "Dump NAND with OOB to file", usbboot_ndump, "<DEVICE> <STARTPAGE> <PAGES> <FILE>" },
48    { "nerase", "Erase NAND blocks", usbboot_nerase, "<DEVICE> <STARTBLOCK> <BLOCKS>" },
49    { "nprogram", "Program NAND from file", usbboot_nprogram, "<DEVICE> <STARTPAGE> <FILE>" },
50    { "nprogram_oob", "Program NAND with OOB from file", usbboot_nprogram, "<DEVICE> <STARTPAGE> <FILE>" },
51    { "nload", "Load NAND data to SDRAM", usbboot_nload, "<DEVICE> <STARTPAGE> <PAGES> <BASE>" },
52    
53    { NULL, NULL, NULL, NULL }
54};
55
56static int usbboot_boot(shell_context_t *ctx, int argc, char *argv[]) {
57    int ret = ingenic_configure_stage2(shell_device(ctx));
58
59    if(ret == -1)
60        perror("ingenic_configure_stage2");
61
62    return ret;
63}
64
65static int usbboot_load(shell_context_t *ctx, int argc, char *argv[]) {
66    int ret = ingenic_load_sdram_file(shell_device(ctx), strtoul(argv[2], NULL, 0), argv[1]);
67
68    if(ret == -1)
69        perror("ingenic_load_sdram_file");
70
71    return ret;
72}
73
74static int usbboot_go(shell_context_t *ctx, int argc, char *argv[]) {
75    int ret = ingenic_go(shell_device(ctx), strtoul(argv[1], NULL, 0));
76
77    if(ret == -1)
78        perror("ingenic_go");
79
80    return ret;
81}
82
83static int usbboot_nquery(shell_context_t *ctx, int argc, char *argv[]) {
84    nand_info_t info;
85
86    int ret = ingenic_query_nand(shell_device(ctx), atoi(argv[1]), &info);
87
88    if(ret == -1) {
89        perror("ingenic_query_nand");
90
91        return -1;
92    }
93
94    printf(
95        "VID: %02hhX\n"
96        "PID: %02hhX\n"
97        "Chip: %02hhX\n"
98        "Page: %02hhX\n"
99        "Plane: %02hhX\n",
100        info.vid, info.pid, info.chip, info.page, info.plane);
101
102
103    return 0;
104}
105
106static int usbboot_ndump(shell_context_t *ctx, int argc, char *argv[]) {
107    int type = strcmp(argv[0], "ndump_oob") ? NO_OOB : OOB_ECC;
108    
109    if(cfg_getenv("NAND_RAW"))
110        type |= NAND_RAW;
111    
112    int ret = ingenic_dump_nand(shell_device(ctx), atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), type, argv[4]);
113    
114    if(ret == -1)
115        perror("ingenic_dump_nand");
116    
117    return ret;
118}
119
120static int usbboot_nerase(shell_context_t *ctx, int argc, char *argv[]) {
121    int ret = ingenic_erase_nand(shell_device(ctx), atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
122    
123    if(ret == -1)
124        perror("ingenic_erase_nand");
125    
126    return ret;
127    
128}
129
130static int usbboot_nprogram(shell_context_t *ctx, int argc, char *argv[]) {
131    int type = strcmp(argv[0], "nprogram_oob") ? NO_OOB : OOB_ECC;
132    
133    if(strcmp(argv[0], "nprogram_oob") == 0) {
134        if(cfg_getenv("NAND_RAW"))
135            type = OOB_ECC;
136        else
137            type = OOB_NO_ECC;
138    } else
139        type = NO_OOB;
140    
141    int ret = ingenic_program_nand(shell_device(ctx), atoi(argv[1]), atoi(argv[2]), type, argv[3]);
142    
143    if(ret == -1)
144        perror("ingenic_program_nand");
145    
146    return ret;
147}
148
149static int usbboot_nload(shell_context_t *ctx, int argc, char *argv[]) {
150    int ret = ingenic_load_nand(shell_device(ctx), atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), strtoul(argv[4], NULL, 0));
151    
152    if(ret == -1)
153        perror("ingenic_load_nand");
154    
155    return ret;
156}
157
158static int usbboot_load_kernel(shell_context_t *ctx, int argc, char *argv[]) {
159    return load_elf(shell_device(ctx), argv[1], argv[2],
160                    argc == 4 ? argv[3] : NULL);
161}
162
163

Archive Download this file



interactive