Date:2010-12-11 22:08:05 (3 years 4 months ago)
Author:Peter Zotov
Commit:6a1f4864e446a72be20744e6f78692d65195478d
Message:Reworked directory structure.

Files: COPYING (1 diff)
Makefile (1 diff)
README (1 diff)
boot.cfg (1 diff)
config.c (1 diff)
config.h (1 diff)
config/boot.cfg (1 diff)
config/initial.cfg (1 diff)
debug.c (1 diff)
debug.h (1 diff)
devmgr.c (1 diff)
devmgr.h (1 diff)
dump.scr (1 diff)
firmware/spl_stage1.bin (0 diffs)
firmware/spl_stage2_usb.bin (0 diffs)
fw.bin (0 diffs)
include/config.h (1 diff)
include/debug.h (1 diff)
include/devmgr.h (1 diff)
include/ingenic.h (1 diff)
include/shell.h (1 diff)
include/shell_internal.h (1 diff)
include/usbdev.h (1 diff)
ingenic.c (1 diff)
ingenic.h (1 diff)
initial.cfg (1 diff)
main.c (1 diff)
script/dump_minios.scr (1 diff)
shell.c (1 diff)
shell.h (1 diff)
shell_builtins.c (1 diff)
shell_internal.h (1 diff)
shell_lex.c (1 diff)
shell_lex.l (1 diff)
spl_cmdset.c (1 diff)
src/Makefile (1 diff)
src/config.c (1 diff)
src/debug.c (1 diff)
src/devmgr.c (1 diff)
src/ingenic.c (1 diff)
src/main.c (1 diff)
src/shell.c (1 diff)
src/shell_builtins.c (1 diff)
src/shell_lex.c (1 diff)
src/shell_lex.l (1 diff)
src/spl_cmdset.c (1 diff)
src/usbboot_cmdset.c (1 diff)
src/usbdev.c (1 diff)
usb_boot.bin (0 diffs)
usbboot_cmdset.c (1 diff)
usbdev.c (1 diff)
usbdev.h (1 diff)

Change Details

COPYING
1fw.bin, usb_boot.bin are (c) Ingenic Semiconductor Co.,Ltd.
1firmware/spl_stage1.bin, firmware/spl_stage2_usb.bin are (c) Ingenic Semiconductor Co.,Ltd.
22ingenic.h is based on code by Ingenic Semiconductor Co.,Ltd.
33
44All other files have their authors and licenses described at their beginning.
Makefile
1READLINE ?= 0
2
3
4ifneq (${READLINE},0)
5    LIBS += -lreadline
6    CPPFLAGS += -DWITH_READLINE
7endif
8
9CC = gcc
10TARGET = jzboot
11SOURCES = debug.c devmgr.c ingenic.c main.c shell_lex.c usbdev.c shell.c shell_builtins.c config.c spl_cmdset.c usbboot_cmdset.c
12CFLAGS = --std=gnu99 -Wall -Werror -O2 $(shell pkg-config libusb-1.0 --cflags) -Wunused-result
13LIBS += $(shell pkg-config libusb-1.0 --libs)
14
15OBJECTS = ${SOURCES:.c=.o}
16
17all: ${TARGET}
18
19${TARGET}: ${OBJECTS}
20    ${CC} ${LDFLAGS} -o $@ $^ ${LIBS}
21
22clean:
23    rm -f ${TARGET} ${OBJECTS} ${SOURCES:.c=.d}
24
25%.o: %.c
26    ${CC} ${CPPFLAGS} ${CFLAGS} -o $@ -MD -c $<
27
28%.c: %.l
29    flex -o $@ $<
30
31-include ${SOURCES:.c=.d}
README
1Needs libusb-1.0 (not 0.1!) to work. Readline is optional.
2To build:
3  make -C src
4If you have readline (recommended):
5  make -C src READLINE=1
boot.cfg
1source initial.cfg
2
3boot
4
config.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23
24#include "config.h"
25#include "debug.h"
26
27char **cfg_environ = NULL;
28
29static int env_size() {
30    int size = 0;
31
32    if(cfg_environ == NULL)
33        return 0;
34
35    for(int i = 0; cfg_environ[i] != NULL; i++)
36        size++;
37
38    return size;
39}
40
41char *cfg_getenv(const char *variable) {
42    if(cfg_environ == NULL)
43        return NULL;
44
45    size_t len = strlen(variable);
46
47    for(int i = 0; cfg_environ[i] != NULL; i++) {
48        char *str = cfg_environ[i], *sep = strchr(str, '=');
49
50        if(sep - str == len && memcmp(str, variable, len) == 0) {
51            return sep + 1;
52        }
53    }
54
55    return NULL;
56}
57
58void cfg_unsetenv(const char *variable) {
59    int size = env_size();
60
61    if(size == 0)
62        return;
63
64    size_t len = strlen(variable);
65
66    for(int i = 0; cfg_environ[i] != NULL; i++) {
67        char *str = cfg_environ[i], *sep = strchr(str, '=');
68
69        if(sep - str == len && memcmp(str, variable, len) == 0) {
70            free(str);
71
72            memcpy(cfg_environ + i, cfg_environ + i + 1, sizeof(char *) * (size - i));
73
74            cfg_environ = realloc(cfg_environ, sizeof(char *) * size);
75
76            return;
77        }
78    }
79}
80
81void cfg_setenv(const char *variable, const char *newval) {
82    int size = env_size();
83
84    size_t len = strlen(variable);
85
86    char *newstr = malloc(len + 1 + strlen(newval) + 1);
87
88    strcpy(newstr, variable);
89    strcat(newstr, "=");
90    strcat(newstr, newval);
91
92    if(size > 0) {
93        for(int i = 0; cfg_environ[i] != NULL; i++) {
94            char *str = cfg_environ[i], *sep = strchr(str, '=');
95
96            if(sep - str == len && memcmp(str, variable, len) == 0) {
97                free(str);
98
99                cfg_environ[i] = newstr;
100
101                return;
102            }
103        }
104    }
105
106    cfg_environ = realloc(cfg_environ, sizeof(char *) * (size + 2));
107
108    cfg_environ[size] = newstr;
109    cfg_environ[size + 1] = NULL;
110}
config.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __CONFIG__H__
20#define __CONFIG__H__
21
22char *cfg_getenv(const char *variable);
23void cfg_setenv(const char *variable, const char *newval);
24void cfg_unsetenv(const char *variable);
25
26extern char **cfg_environ;
27
28#endif
config/boot.cfg
1source script/initial.cfg
2
3boot
4
config/initial.cfg
1# Configuration variables:
2# STAGE1_FILE, STAGE2_FILE
3
4set STAGE1_FILE firmware/spl_stage1.bin
5set STAGE2_FILE firmware/spl_stage2_usb.bin
6
7set EXTCLK 12 # Define the external crystal in MHz
8set CPUSPEED 252 # Define the PLL output frequency
9set PHMDIV 3 # Define the frequency divider ratio of PLL=CCLK:PCLK=HCLK=MCLK
10set BAUDRATE 57600 # Define the uart baudrate
11set USEUART 0 # UART number
12
13set SDRAM_BUSWIDTH 16 # The bus width of the SDRAM in bits (16|32)
14set SDRAM_BANKS 4 # The bank number (2|4)
15set SDRAM_ROWADDR 13 # Row address width in bits (11-13)
16set SDRAM_COLADDR 9 # Column address width in bits (8-12)
17set SDRAM_ISMOBILE 0 # Define whether SDRAM is mobile SDRAM (only or Jz4750), 1: yes 0: no
18set SDRAM_ISBUSSHARE 1 # Define whether SDRAM bus share with NAND 1:shared 0:unshared
19
20set NAND_BUSWIDTH 8 # The width of the NAND flash chip in bits (8|16|32)
21set NAND_ROWCYCLES 3 # The row address cycles (2|3)
22set NAND_PAGESIZE 2048 # The page size of the NAND chip in bytes(512|2048|4096)
23set NAND_PAGEPERBLOCK 128 # The page number per block
24set NAND_FORCEERASE 1 # The force to erase flag (0|1)
25set NAND_OOBSIZE 64 # OOB size in byte
26set NAND_ECCPOS 8 # Specify the ECC offset inside the oob data (0-[oobsize-1])
27set NAND_BADBLOCKPOS 0 # Specify the badblock flag offset inside the oob (0-[oobsize-1])
28set NAND_BADBLOCKPAGE 0 # Specify the page number of badblock flag inside a block(0-[PAGEPERBLOCK-1])
29set NAND_PLANENUM 1 # The planes number of target nand flash
30set NAND_BCHBIT 8 # Specify the hardware BCH algorithm for 4750 (4|8)
31set NAND_WPPIN 0 # Specify the write protect pin number
32set NAND_BLOCKPERCHIP 4096 # Specify the block number per chip,0 means ignore
33
34rebuildcfg
debug.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdarg.h>
20#include <stdio.h>
21
22#include "debug.h"
23
24static int debug_level = LEVEL_ERROR;
25
26void set_debug_level(int level) {
27    debug_level = level;
28}
29
30int get_debug_level() {
31    return debug_level;
32}
33
34void debug(int level, const char *fmt, ...) {
35    va_list list;
36
37    va_start(list, fmt);
38
39    if(level <= debug_level) {
40        if(level <= LEVEL_ERROR)
41            vfprintf(stderr, fmt, list);
42        else
43            vprintf(fmt, list);
44    }
45
46    va_end(list);
47}
debug.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __DEBUG__H__
20#define __DEBUG__H__
21
22void set_debug_level(int level);
23int get_debug_level();
24
25void debug(int level, const char *fmt, ...);
26
27#define LEVEL_SILENT 0
28#define LEVEL_ERROR 1
29#define LEVEL_WARNING 2
30#define LEVEL_INFO 3
31#define LEVEL_DEBUG 4
32
33#endif
devmgr.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdlib.h>
20
21#include "devmgr.h"
22#include "debug.h"
23
24typedef struct device {
25    struct device *next;
26    uint16_t vid;
27    uint16_t pid;
28    void *data;
29} usb_device_t;
30
31static const struct {
32    uint16_t vid;
33    uint16_t pid;
34} devices[] = {
35    { 0x601a, 0x4740 },
36    { 0x601a, 0x4750 },
37    { 0x601a, 0x4760 },
38    { 0, 0 }
39};
40
41static usb_device_t *list = NULL;
42
43int is_ingenic(uint16_t vid, uint16_t pid) {
44    for(int i = 0; devices[i].vid != 0; i++)
45        if(devices[i].vid == vid && devices[i].pid == pid)
46            return 1;
47
48    return 0;
49}
50
51void add_device(uint16_t vid, uint16_t pid, void *data) {
52    usb_device_t *newdev = malloc(sizeof(usb_device_t));
53
54    newdev->next = list;
55    newdev->vid = vid;
56    newdev->pid = pid;
57    newdev->data = data;
58
59    list = newdev;
60
61    debug(LEVEL_DEBUG, "Device manager: registered %04hX:%04hX with data %p\n", vid, pid, data);
62}
63
64void enum_devices(void (*handler)(int idx, uint16_t vid, uint16_t pid, void *data)) {
65    int idx = 0;
66
67    for(usb_device_t *dev = list; dev; dev = dev->next)
68        handler(idx++, dev->vid, dev->pid, dev->data);
69}
70
71void *get_device(int idx) {
72    for(usb_device_t *dev = list; dev; dev = dev->next)
73        if(idx-- == 0)
74            return dev->data;
75
76    return NULL;
77}
devmgr.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __DEVMGR__H__
20#define __DEVMGR__H__
21
22#include <stdint.h>
23
24#define INTERFACE_BOOT 0
25#define ENDPOINT_OUT 0x01
26#define ENDPOINT_IN 0x81
27
28int is_ingenic(uint16_t vid, uint16_t pid);
29
30void add_device(uint16_t vid, uint16_t pid, void *data);
31void enum_devices(void (*handler)(int idx, uint16_t vid, uint16_t pid, void *data));
32void *get_device(int idx);
33
34#endif
dump.scr
1echo "Dumping script"
2nquery 0
3
4#set NAND_RAW 1
5set NAND_ECCPOS 3
6rebuildcfg
7boot
8
9echo "Configured for bootloader IO!"
10
11ndump_oob 0 0 128 "dump/nand.bin"
12
13set NAND_RAW
14set NAND_ECCPOS 8
15rebuildcfg
16boot
17
18echo "Configuration restored"
19
20ndump 0 128 66 "dump/loader.bin"
21ndump 0 256 1 "dump/def_boot.bin"
22ndump 0 512 3061 "dump/img_boot.bin"
23ndump 0 8192 8192 "dump/minios.bin"
24ndump 0 32768 32768 "dump/res.bin"
25
firmware/spl_stage1.bin
firmware/spl_stage2_usb.bin
fw.bin
include/config.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __CONFIG__H__
20#define __CONFIG__H__
21
22char *cfg_getenv(const char *variable);
23void cfg_setenv(const char *variable, const char *newval);
24void cfg_unsetenv(const char *variable);
25
26extern char **cfg_environ;
27
28#endif
include/debug.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __DEBUG__H__
20#define __DEBUG__H__
21
22void set_debug_level(int level);
23int get_debug_level();
24
25void debug(int level, const char *fmt, ...);
26
27#define LEVEL_SILENT 0
28#define LEVEL_ERROR 1
29#define LEVEL_WARNING 2
30#define LEVEL_INFO 3
31#define LEVEL_DEBUG 4
32
33#endif
include/devmgr.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __DEVMGR__H__
20#define __DEVMGR__H__
21
22#include <stdint.h>
23
24#define INTERFACE_BOOT 0
25#define ENDPOINT_OUT 0x01
26#define ENDPOINT_IN 0x81
27
28int is_ingenic(uint16_t vid, uint16_t pid);
29
30void add_device(uint16_t vid, uint16_t pid, void *data);
31void enum_devices(void (*handler)(int idx, uint16_t vid, uint16_t pid, void *data));
32void *get_device(int idx);
33
34#endif
include/ingenic.h
1/* This file is based on code by Ingenic Semiconductor Co., Ltd. */
2
3#ifndef __INGENIC__H__
4#define __INGENIC__H__
5
6#include <stdint.h>
7
8#define VR_GET_CPU_INFO 0x00
9#define VR_SET_DATA_ADDRESS 0x01
10#define VR_SET_DATA_LENGTH 0x02
11#define VR_FLUSH_CACHES 0x03
12#define VR_PROGRAM_START1 0x04
13#define VR_PROGRAM_START2 0x05
14#define VR_NOR_OPS 0x06
15#define VR_NAND_OPS 0x07
16#define VR_SDRAM_OPS 0x08
17#define VR_CONFIGRATION 0x09
18#define VR_GET_NUM 0x0a
19
20#define CMDSET_SPL 1
21#define CMDSET_USBBOOT 2
22
23#define INGENIC_STAGE1 1
24#define INGENIC_STAGE2 2
25
26#define STAGE1_DEBUG_BOOT 0
27#define STAGE1_DEBUG_MEMTEST 1
28#define STAGE1_DEBUG_GPIO_SET 2
29#define STAGE1_DEBUG_GPIO_CLEAR 3
30
31#define STAGE1_BASE 0x2000
32#define STAGE2_CODESIZE 0x400000
33#define SDRAM_BASE 0x80000000
34
35#define STAGE2_IOBUF (2048 * 128)
36
37#define DS_flash_info 0
38#define DS_hand 1
39
40#define SDRAM_LOAD 0
41
42#define NAND_QUERY 0
43#define NAND_INIT 1
44#define NAND_MARK_BAD 2
45#define NAND_READ_OOB 3
46#define NAND_READ_RAW 4
47#define NAND_ERASE 5
48#define NAND_READ 6
49#define NAND_PROGRAM 7
50#define NAND_READ_TO_RAM 8
51
52#define OOB_ECC 0
53#define OOB_NO_ECC 1
54#define NO_OOB 2
55#define NAND_RAW (1 << 7)
56
57#define PROGRESS_INIT 0
58#define PROGRESS_UPDATE 1
59#define PROGRESS_FINI 2
60
61typedef struct {
62    /* debug args */
63    uint8_t debug_ops;
64    uint8_t pin_num;
65    uint32_t start;
66    uint32_t size;
67} __attribute__((packed)) ingenic_stage1_debug_t;
68
69typedef struct {
70    /* CPU ID */
71    uint32_t cpu_id;
72
73    /* PLL args */
74    uint8_t ext_clk;
75    uint8_t cpu_speed;
76    uint8_t phm_div;
77    uint8_t use_uart;
78    uint32_t baudrate;
79
80    /* SDRAM args */
81    uint8_t bus_width;
82    uint8_t bank_num;
83    uint8_t row_addr;
84    uint8_t col_addr;
85    uint8_t is_mobile;
86    uint8_t is_busshare;
87
88    ingenic_stage1_debug_t debug;
89} __attribute__((packed)) firmware_config_t;
90
91typedef struct {
92    /* nand flash info */
93    uint32_t cpuid; /* cpu type */
94    uint32_t nand_bw; /* bus width */
95    uint32_t nand_rc; /* row cycle */
96    uint32_t nand_ps; /* page size */
97    uint32_t nand_ppb; /* page number per block */
98    uint32_t nand_force_erase;
99    uint32_t nand_pn; /* page number in total */
100    uint32_t nand_os; /* oob size */
101    uint32_t nand_eccpos;
102    uint32_t nand_bbpage;
103    uint32_t nand_bbpos;
104    uint32_t nand_plane;
105    uint32_t nand_bchbit;
106    uint32_t nand_wppin;
107    uint32_t nand_bpc; /* block number per chip */
108} nand_config_t;
109
110typedef struct {
111    uint8_t vid;
112    uint8_t pid;
113    uint8_t chip;
114    uint8_t page;
115    uint8_t plane;
116} nand_info_t;
117
118
119typedef struct {
120    void (*cmdset_change)(uint32_t cmdset, void *arg);
121    void (*progress)(int action, int value, int max, void *arg);
122} ingenic_callbacks_t;
123
124void *ingenic_open(void *usb_hndl);
125void ingenic_close(void *hndl);
126void ingenic_set_callbacks(void *hndl, const ingenic_callbacks_t *callbacks, void *arg);
127
128int ingenic_redetect(void *hndl);
129int ingenic_cmdset(void *hndl);
130int ingenic_type(void *hndl);
131uint32_t ingenic_sdram_size(void *hndl);
132
133int ingenic_rebuild(void *hndl);
134int ingenic_loadstage(void *hndl, int id, const char *filename);
135int ingenic_stage1_debugop(void *device, const char *filename, uint32_t op, uint32_t pin, uint32_t base, uint32_t size);
136int ingenic_memtest(void *hndl, const char *filename, uint32_t base, uint32_t size, uint32_t *fail);
137
138int ingenic_configure_stage2(void *hndl);
139int ingenic_load_sdram(void *hndl, void *data, uint32_t base, uint32_t size);
140int ingenic_load_sdram_file(void *hndl, uint32_t base, const char *filename);
141int ingenic_go(void *hndl, uint32_t address);
142
143int ingenic_query_nand(void *hndl, int cs, nand_info_t *info);
144int ingenic_dump_nand(void *hndl, int cs, int start, int pages, int type, const char *filename);
145int ingenic_program_nand(void *hndl, int cs, int start, int type, const char *filename);
146int ingenic_erase_nand(void *hndl, int cs, int start, int blocks);
147int ingenic_load_nand(void *hndl, int cs, int start, int pages, uint32_t base);
148
149#endif
include/shell.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __SHELL__H__
20#define __SHELL__H__
21
22#ifndef SHELL_INTERNALS
23typedef void shell_context_t;
24#endif
25
26typedef struct shell_command {
27    const char *cmd;
28    const char *description;
29    int (*handler)(shell_context_t *ctx, int argc, char *argv[]);
30    const char *args;
31} shell_command_t;
32
33shell_context_t *shell_init(void *ingenic);
34void shell_fini(shell_context_t *context);
35
36void shell_interactive(shell_context_t *ctx);
37int shell_source(shell_context_t *ctx, const char *filename);
38int shell_execute(shell_context_t *ctx, const char *input);
39void *shell_device(shell_context_t *ctx);
40int shell_run(shell_context_t *ctx, int argc, char *argv[]);
41
42void shell_exit(shell_context_t *ctx, int val);
43int shell_enumerate_commands(shell_context_t *ctx, int (*callback)(shell_context_t *ctx, const shell_command_t *cmd, void *arg), void *arg);
44
45extern const shell_command_t spl_cmdset[];
46extern const shell_command_t usbboot_cmdset[];
47extern const shell_command_t builtin_cmdset[];
48
49#endif
include/shell_internal.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __SHELL_INTERNAL__H__
20#define __SHELL_INTERNAL__H__
21
22#define SHELL_INTERNALS
23#define STATE_WANTSTR 0
24#define STATE_WANTSPACE 1
25
26#define TOK_SEPARATOR 1
27#define TOK_STRING 2
28#define TOK_SPACE 3
29#define TOK_COMMENT 4
30
31typedef struct {
32    void *device;
33    char linebuf[512];
34    char *strval;
35    char *line;
36    const struct shell_command *set_cmds;
37    int shell_exit;
38    int prev_progress;
39} shell_context_t;
40
41
42typedef struct {
43    int argc;
44    char **argv;
45} shell_run_data_t;
46
47int shell_pull(shell_context_t *ctx, char *buf, int maxlen);
48
49#ifndef FLEX_SCANNER
50typedef void *yyscan_t;
51int yylex_init(yyscan_t *ptr_yy_globals);
52int yylex_init_extra(shell_context_t *user_defined, yyscan_t *ptr_yy_globals);
53int yylex(yyscan_t yyscanner) ;
54int yylex_destroy (yyscan_t yyscanner) ;
55#else
56#define YY_EXTRA_TYPE shell_context_t *
57#define YY_INPUT(buf, result, max_size) result = shell_pull(yyextra, buf, max_size);
58#endif
59
60#include "shell.h"
61
62#endif
63
include/usbdev.h
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#ifndef __USBDEV__H__
21#define __USBDEV__H__
22
23#include <stdint.h>
24
25int usbdev_init();
26void usbdev_fini();
27
28#define USBDEV_FROMDEV 0
29#define USBDEV_TODEV 1
30
31int usbdev_enumerate();
32void *usbdev_open(void *dev);
33void usbdev_close(void *hndl);
34int usbdev_vendor(void *hndl, int direction, uint8_t req, uint16_t value, uint16_t index, void *data, uint16_t size);
35int usbdev_sendbulk(void *hndl, void *data, int size);
36int usbdev_recvbulk(void *hndl, void *data, int size);
37
38#endif
ingenic.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 <stdlib.h>
21#include <errno.h>
22#include <string.h>
23#include <stdio.h>
24#include <unistd.h>
25#include <ctype.h>
26
27#include "ingenic.h"
28#include "usbdev.h"
29#include "debug.h"
30#include "config.h"
31
32#define HANDLE ingenic_handle_t *handle = hndl
33#define BUILDTYPE(cmdset, id) (((cmdset) << 16) | (0x##id & 0xFFFF))
34#define CPUID(id) ((id) & 0xFFFF)
35#define CMDSET(id) (((id) & 0xFFFF0000) >> 16)
36
37#define CFGOPT(name, var, exp) { char *str = cfg_getenv(name); if(str == NULL) { debug(LEVEL_ERROR, "%s is not set\n", name); errno = EINVAL; return -1; }; int v = atoi(str); if(!(exp)) { debug(LEVEL_ERROR, "%s must be in %s\n", name, #exp); return -1; }; handle->cfg.var = v; }
38
39#define NOPT(name, var, exp) { char *str = cfg_getenv("NAND_" name); if(str == NULL) { debug(LEVEL_ERROR, "%s is not set\n", "NAND_" name); errno = EINVAL; return -1; }; int v = atoi(str); if(!(exp)) { debug(LEVEL_ERROR, "%s must be in %s\n", "NAND_" name, #exp); return -1; }; handle->nand.nand_##var = v; }
40
41#define CALLBACK(function, ...) if(handle->callbacks && handle->callbacks->function) handle->callbacks->function(__VA_ARGS__, handle->callbacks_data)
42
43typedef struct {
44    void *usb;
45    uint32_t type;
46    uint32_t total_sdram_size;
47
48    const ingenic_callbacks_t *callbacks;
49    void *callbacks_data;
50
51    firmware_config_t cfg;
52    nand_config_t nand;
53} ingenic_handle_t;
54
55static const struct {
56    const char * const magic;
57    uint32_t id;
58} magic_list[] = {
59    { "JZ4740V1", BUILDTYPE(CMDSET_SPL, 4740) },
60    { "JZ4750V1", BUILDTYPE(CMDSET_SPL, 4750) },
61    { "JZ4760V1", BUILDTYPE(CMDSET_SPL, 4760) },
62
63    { "Boot4740", BUILDTYPE(CMDSET_USBBOOT, 4740) },
64    { "Boot4750", BUILDTYPE(CMDSET_USBBOOT, 4750) },
65    { "Boot4760", BUILDTYPE(CMDSET_USBBOOT, 4760) },
66
67    { NULL, 0 }
68};
69
70static void hexdump(const void *data, size_t size) {
71        const unsigned char *bytes = data;
72
73        for(int i = 0; i < size; i+= 16) {
74                debug(LEVEL_DEBUG, "%04X ", i);
75
76                int chunk_size = size - i;
77        if(chunk_size > 16)
78            chunk_size = 16;
79
80                for(int j = 0; j < chunk_size; j++) {
81                        debug(LEVEL_DEBUG, "%02X ", bytes[i + j]);
82
83                        if(j == 7)
84                                debug(LEVEL_DEBUG, " ");
85                }
86
87                for(int j = 0; j < 16 - chunk_size; j++) {
88                        debug(LEVEL_DEBUG, " ");
89
90                        if(j == 8)
91                                debug(LEVEL_DEBUG, " ");
92                }
93
94                debug(LEVEL_DEBUG, "|");
95
96                for(int j = 0; j < chunk_size; j++) {
97                        debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.');
98                }
99
100                debug(LEVEL_DEBUG, "|\n");
101        }
102}
103
104static uint32_t ingenic_probe(void *usb_hndl) {
105    char magic[9];
106
107    if(usbdev_vendor(usb_hndl, USBDEV_FROMDEV, VR_GET_CPU_INFO, 0, 0, magic, 8) == -1)
108            return 0;
109
110    magic[8] = 0;
111
112    for(int i = 0; magic_list[i].magic != NULL; i++)
113        if(strcmp(magic_list[i].magic, magic) == 0)
114            return magic_list[i].id;
115
116    debug(LEVEL_ERROR, "Unknown CPU: '%s'\n", magic);
117    errno = EINVAL;
118    return 0;
119}
120
121void *ingenic_open(void *usb_hndl) {
122    uint32_t type = ingenic_probe(usb_hndl);
123
124    if(type == 0)
125        return NULL;
126
127    ingenic_handle_t *ret = malloc(sizeof(ingenic_handle_t));
128    ret->usb = usb_hndl;
129    ret->type = type;
130    ret->callbacks = NULL;
131
132    return ret;
133}
134
135int ingenic_redetect(void *hndl) {
136    HANDLE;
137
138    uint32_t type = ingenic_probe(handle->usb);
139
140    if(type == 0)
141        return -1;
142
143    uint32_t prev = handle->type;
144
145    handle->type = type;
146
147    if(CMDSET(prev) != CMDSET(type)) {
148        CALLBACK(cmdset_change, CMDSET(type));
149    }
150
151    return 0;
152}
153
154void ingenic_set_callbacks(void *hndl, const ingenic_callbacks_t *callbacks, void *arg) {
155    HANDLE;
156
157    handle->callbacks = callbacks;
158    handle->callbacks_data = arg;
159}
160
161int ingenic_cmdset(void *hndl) {
162    HANDLE;
163
164    return CMDSET(handle->type);
165}
166
167int ingenic_type(void *hndl) {
168    HANDLE;
169
170    return CPUID(handle->type);
171}
172
173void ingenic_close(void *hndl) {
174    HANDLE;
175
176    free(handle);
177}
178
179int ingenic_rebuild(void *hndl) {
180    HANDLE;
181
182    handle->cfg.cpu_id = CPUID(handle->type);
183
184    CFGOPT("EXTCLK", ext_clk, v <= 27 && v >= 12);
185    CFGOPT("CPUSPEED", cpu_speed, (v % 12) == 0);
186    handle->cfg.cpu_speed /= handle->cfg.ext_clk;
187    CFGOPT("PHMDIV", phm_div, v <= 32 && v >= 2);
188    CFGOPT("USEUART", use_uart, 1);
189    CFGOPT("BAUDRATE", baudrate, 1);
190
191    CFGOPT("SDRAM_BUSWIDTH", bus_width, (v == 16) || (v == 32));
192    handle->cfg.bus_width = handle->cfg.bus_width == 16 ? 1 : 0;
193    CFGOPT("SDRAM_BANKS", bank_num, (v >= 4) && ((v % 4) == 0));
194    handle->cfg.bank_num /= 4;
195    CFGOPT("SDRAM_ROWADDR", row_addr, 1);
196    CFGOPT("SDRAM_COLADDR", col_addr, 1);
197    CFGOPT("SDRAM_ISMOBILE", is_mobile, v == 0 || v == 1);
198    CFGOPT("SDRAM_ISBUSSHARE", is_busshare, v == 0 || v == 1);
199
200    memset(&handle->cfg.debug, 0, sizeof(ingenic_stage1_debug_t));
201
202    handle->total_sdram_size = (uint32_t)
203        (2 << (handle->cfg.row_addr + handle->cfg.col_addr - 1)) * 2
204        * (handle->cfg.bank_num + 1) * 2
205        * (2 - handle->cfg.bus_width);
206
207    handle->nand.cpuid = CPUID(handle->type);
208
209    NOPT("BUSWIDTH", bw, 1);
210    NOPT("ROWCYCLES", rc, 1);
211    NOPT("PAGESIZE", ps, 1);
212    NOPT("PAGEPERBLOCK", ppb, 1);
213    NOPT("FORCEERASE", force_erase, 1);
214// FIXME: pn is not set by xburst-tools usbboot. Is this intended?
215    NOPT("OOBSIZE", os, 1);
216    NOPT("ECCPOS", eccpos, 1);
217    NOPT("BADBLOCKPOS", bbpos, 1);
218    NOPT("BADBLOCKPAGE", bbpage, 1);
219    NOPT("PLANENUM", plane, 1);
220    NOPT("BCHBIT", bchbit, 1);
221    NOPT("WPPIN", wppin, 1);
222    NOPT("BLOCKPERCHIP", bpc, 1);
223
224    return 0;
225}
226
227uint32_t ingenic_sdram_size(void *hndl) {
228    HANDLE;
229
230    return handle->total_sdram_size;
231}
232
233static int ingenic_wordop(void *usb, int op, uint32_t base) {
234    return usbdev_vendor(usb, USBDEV_TODEV, op, (base >> 16), base & 0xFFFF, 0, 0);
235}
236
237int ingenic_stage1_debugop(void *hndl, const char *filename, uint32_t op, uint32_t pin, uint32_t base, uint32_t size) {
238    HANDLE;
239
240    handle->cfg.debug.debug_ops = op;
241    handle->cfg.debug.pin_num = pin;
242    handle->cfg.debug.start = base;
243    handle->cfg.debug.size = size;
244
245    int ret = ingenic_loadstage(handle, INGENIC_STAGE1, filename);
246
247    memset(&handle->cfg.debug, 0, sizeof(ingenic_stage1_debug_t));
248
249    return ret;
250}
251
252int ingenic_loadstage(void *hndl, int id, const char *file) {
253    HANDLE;
254
255    if(file == NULL) {
256        errno = EINVAL;
257
258        return -1;
259    }
260
261    uint32_t base;
262    int cmd;
263
264    switch(id) {
265    case INGENIC_STAGE1:
266        base = SDRAM_BASE + STAGE1_BASE;
267        cmd = VR_PROGRAM_START1;
268
269        break;
270
271    case INGENIC_STAGE2:
272        base = SDRAM_BASE + handle->total_sdram_size - STAGE2_CODESIZE;
273        cmd = VR_PROGRAM_START2;
274
275        break;
276
277    default:
278        errno = EINVAL;
279
280        return -1;
281    }
282
283    FILE *fd = fopen(file, "rb");
284
285    if(fd == NULL)
286        return -1;
287
288    fseek(fd, 0, SEEK_END);
289    int size = ftell(fd);
290    fseek(fd, 0, SEEK_SET);
291
292    void *data = malloc(size);
293    size_t read_bytes = fread(data, 1, size, fd);
294
295    fclose(fd);
296
297    if(read_bytes != size) {
298        free(data);
299
300        errno = EIO;
301
302        return -1;
303    }
304
305    memcpy(data + 8, &handle->cfg, sizeof(firmware_config_t));
306
307    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, base) == -1) {
308        free(data);
309
310        return -1;
311    }
312
313    int ret = usbdev_sendbulk(handle->usb, data, size);
314
315    free(data);
316
317    if(ret == -1)
318        return -1;
319
320    if(id == INGENIC_STAGE2) {
321        ret = usbdev_vendor(handle->usb, USBDEV_TODEV, VR_FLUSH_CACHES, 0, 0, 0, 0);
322
323        if(ret == -1)
324            return -1;
325    }
326
327    ret = usbdev_vendor(handle->usb, USBDEV_TODEV, cmd, (base >> 16), base & 0xFFFF, 0, 0);
328
329    if(ret == -1)
330        return -1;
331
332    usleep(250);
333
334    if(id == INGENIC_STAGE2)
335        return ingenic_redetect(hndl);
336    else
337        return 0;
338}
339
340int ingenic_memtest(void *hndl, const char *filename, uint32_t base, uint32_t size, uint32_t *fail) {
341    HANDLE;
342
343    int ret = ingenic_stage1_debugop(handle, filename, STAGE1_DEBUG_MEMTEST, 0, base, size);
344
345    if(ret == -1)
346        return -1;
347
348    uint32_t data[2];
349
350    ret = usbdev_recvbulk(handle->usb, &data, sizeof(data));
351
352    if(ret == -1)
353        return -1;
354
355    hexdump(data, ret);
356
357    if(ret < 4) {
358        errno = EIO;
359
360        return -1;
361    }
362
363    if(data[0] != 0) {
364        errno = EFAULT;
365
366        *fail = data[0];
367
368        return -1;
369    }
370
371    return 0;
372}
373
374int ingenic_configure_stage2(void *hndl) {
375    HANDLE;
376
377// DS_flash_info (nand_config_t only) is not implemented in stage2, so using DS_hand (nand_config_t + firmware_config_t)
378    uint8_t *hand = malloc(sizeof(nand_config_t) + sizeof(firmware_config_t));
379
380    memcpy(hand, &handle->nand, sizeof(nand_config_t));
381    memcpy(hand + sizeof(nand_config_t), &handle->cfg, sizeof(firmware_config_t));
382
383    int ret = usbdev_sendbulk(handle->usb, hand, sizeof(nand_config_t) + sizeof(firmware_config_t));
384
385    free(hand);
386
387    if(ret == -1)
388        return -1;
389
390    if(usbdev_vendor(handle->usb, USBDEV_TODEV, VR_CONFIGRATION, DS_hand, 0, 0, 0) == -1)
391        return -1;
392
393    uint32_t result[8];
394
395    ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
396
397    if(ret == -1)
398        return -1;
399
400    return 0;
401}
402
403int ingenic_load_sdram(void *hndl, void *data, uint32_t base, uint32_t size) {
404    HANDLE;
405
406    int max = size, value = 0;
407
408    CALLBACK(progress, PROGRESS_INIT, 0, max);
409
410    while(size) {
411        int block = size > STAGE2_IOBUF ? STAGE2_IOBUF : size;
412
413        debug(LEVEL_DEBUG, "Loading %d bytes from %p to 0x%08X\n", block, data, base);
414
415        if(usbdev_sendbulk(handle->usb, data, block) == -1)
416            return -1;
417
418        if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, base) == -1)
419            return -1;
420
421        if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, block) == -1)
422            return -1;
423
424
425        if(usbdev_vendor(handle->usb, USBDEV_TODEV, VR_SDRAM_OPS, SDRAM_LOAD, 0, 0, 0) == -1)
426            return -1;
427
428        uint32_t result[8];
429
430        int ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
431        if(ret == -1)
432            return -1;
433
434        hexdump(result, ret);
435
436        data += block;
437        base += block;
438        size -= block;
439        value += block;
440
441        CALLBACK(progress, PROGRESS_UPDATE, value, max);
442
443    }
444
445    CALLBACK(progress, PROGRESS_FINI, 0, 0);
446
447    debug(LEVEL_DEBUG, "Load done\n");
448
449    return 0;
450}
451
452int ingenic_load_sdram_file(void *hndl, uint32_t base, const char *file) {
453    HANDLE;
454
455    FILE *fd = fopen(file, "rb");
456
457    if(fd == NULL)
458        return -1;
459
460    fseek(fd, 0, SEEK_END);
461    int size = ftell(fd);
462    fseek(fd, 0, SEEK_SET);
463
464    void *data = malloc(size);
465    size_t bytes = fread(data, 1, size, fd);
466
467    fclose(fd);
468
469    if(bytes != size) {
470        free(data);
471
472        errno = EIO;
473
474        return -1;
475    }
476
477    int ret = ingenic_load_sdram(handle, data, base, size);
478
479    free(data);
480
481    return ret;
482}
483
484int ingenic_go(void *hndl, uint32_t address) {
485    HANDLE;
486
487    debug(LEVEL_DEBUG, "Go to 0x%08X\n", address);
488
489    return ingenic_wordop(handle->usb, VR_PROGRAM_START2, address);
490}
491
492static inline int ingenic_nandop(void *usb, uint8_t cs, uint8_t request, uint8_t param) {
493    return usbdev_vendor(usb, USBDEV_TODEV, VR_NAND_OPS, (param << 12) | (cs << 4) | (request & 0x0F), 0, 0, 0);
494}
495
496int ingenic_query_nand(void *hndl, int cs, nand_info_t *info) {
497    HANDLE;
498
499    if(ingenic_nandop(handle->usb, cs, NAND_QUERY, 0) == -1)
500        return -1;
501
502    uint32_t dummy[8];
503
504    int ret = usbdev_recvbulk(handle->usb, dummy, sizeof(dummy));
505
506    if(ret == -1)
507        return -1;
508
509    if(ret < sizeof(nand_info_t)) {
510        errno = EIO;
511
512        return -1;
513    }
514
515    memcpy(info, dummy, sizeof(nand_info_t));
516
517    if(usbdev_recvbulk(handle->usb, dummy, sizeof(dummy)) == -1)
518        return -1;
519
520    return 0;
521}
522
523int ingenic_dump_nand(void *hndl, int cs, int start, int pages, int type, const char *filename) {
524    HANDLE;
525
526    int raw = type & NAND_RAW;
527    type &= ~NAND_RAW;
528
529    int page_size = (handle->nand.nand_ps + (type == NO_OOB ? 0 : handle->nand.nand_os));
530    int chunk_pages = STAGE2_IOBUF / page_size;
531
532    FILE *dest = fopen(filename, "wb");
533
534    if(dest == NULL)
535        return -1;
536
537    void *iobuf = malloc(chunk_pages * page_size);
538
539    int ret = 0;
540    int value = 0, max = pages;
541
542    CALLBACK(progress, PROGRESS_INIT, 0, max);
543
544    while(pages > 0) {
545        int chunk = pages < chunk_pages ? pages : chunk_pages;
546        int bytes = chunk * page_size;
547
548        ret = ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start);
549
550        if(ret == -1)
551            break;
552
553        ret = ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, chunk);
554
555        if(ret == -1)
556            break;
557
558        ret = ingenic_nandop(handle->usb, cs, raw ? NAND_READ_RAW : NAND_READ, type);
559
560        if(ret == -1)
561            break;
562
563        int ret = usbdev_recvbulk(handle->usb, iobuf, bytes);
564
565        if(ret == -1)
566            return -1;
567
568        if(ret != bytes) {
569            debug(LEVEL_ERROR, "Ingenic: NAND dump truncated: expected %d bytes, received %d\n", bytes, ret);
570
571            errno = EIO;
572
573            break;
574        }
575
576        uint16_t result[4];
577
578
579        ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
580
581        if(ret == -1)
582            return -1;
583
584        if(result[3] != 0 && !raw) {
585            debug(LEVEL_ERROR, "Ingenic: ECC failure while reading NAND. See UART output for details\n");
586
587            errno = EIO;
588
589            break;
590        }
591
592        fwrite(iobuf, bytes, 1, dest);
593
594        start += chunk;
595        pages -= chunk;
596
597        value += chunk;
598
599        CALLBACK(progress, PROGRESS_UPDATE, value, max);
600    }
601
602    free(iobuf);
603    fclose(dest);
604
605    CALLBACK(progress, PROGRESS_FINI, 0, 0);
606
607    return ret;
608}
609
610int ingenic_erase_nand(void *hndl, int cs, int start, int blocks) {
611    HANDLE;
612
613    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start) == -1)
614        return -1;
615
616    if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, blocks) == -1)
617        return -1;
618
619    if(ingenic_nandop(handle->usb, cs, NAND_ERASE, 0) == -1)
620        return -1;
621
622    uint16_t result[4];
623
624    int ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
625
626    if(ret == -1)
627        return -1;
628
629    hexdump(result, ret);
630
631    return 0;
632}
633
634int ingenic_program_nand(void *hndl, int cs, int start, int type, const char *filename) {
635    HANDLE;
636
637    int page_size = (handle->nand.nand_ps + (type == NO_OOB ? 0 : handle->nand.nand_os));
638    int chunk_pages = STAGE2_IOBUF / page_size;
639
640    FILE *in = fopen(filename, "rb");
641
642    if(in == NULL)
643        return -1;
644
645    fseek(in, 0, SEEK_END);
646    int file_size = ftell(in);
647    fseek(in, 0, SEEK_SET);
648
649    int tail = file_size % page_size;
650
651    if(tail) {
652        tail = page_size - tail;
653
654        file_size += tail;
655    }
656
657    int pages = file_size / page_size;
658    int ret = 0;
659
660    void *iobuf = malloc(chunk_pages * page_size);
661
662    debug(LEVEL_INFO, "Programming %d pages from %d (%d bytes, %d bytes/page)\n", pages, start, file_size, page_size);
663
664    int value = 0, max = pages;
665
666    CALLBACK(progress, PROGRESS_INIT, 0, max);
667
668    while(pages > 0) {
669        int chunk = pages < chunk_pages ? pages : chunk_pages;
670        int bytes = chunk * page_size;
671
672        ret = fread(iobuf, 1, bytes, in);
673
674        if(pages < chunk_pages && tail) {
675            memset(iobuf + ret, 0xFF, tail);
676
677            ret += tail;
678        }
679
680        if(ret != bytes) {
681            debug(LEVEL_ERROR, "fread: %d\n", ret);
682
683            ret = -1;
684            errno = EIO;
685
686            break;
687        }
688
689        if(usbdev_sendbulk(handle->usb, iobuf, bytes) == -1)
690            return -1;
691
692        ret = ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start);
693
694        if(ret == -1)
695            break;
696
697        ret = ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, chunk);
698
699        if(ret == -1)
700            break;
701
702        ret = ingenic_nandop(handle->usb, cs, NAND_PROGRAM, type);
703
704        if(ret == -1)
705            break;
706
707        uint16_t result[4];
708
709        ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
710
711        if(ret == -1)
712            return -1;
713
714        hexdump(result, ret);
715
716        start += chunk;
717        pages -= chunk;
718        value += chunk;
719
720        CALLBACK(progress, PROGRESS_UPDATE, value, max);
721    }
722
723    free(iobuf);
724    fclose(in);
725
726    CALLBACK(progress, PROGRESS_FINI, 0, 0);
727
728    return ret;
729}
730
731int ingenic_load_nand(void *hndl, int cs, int start, int pages, uint32_t base) {
732    HANDLE;
733
734    if(ingenic_wordop(handle->usb, VR_PROGRAM_START1, base) == -1)
735        return -1;
736
737    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start) == -1)
738        return -1;
739
740    if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, pages) == -1)
741        return -1;
742
743    if(ingenic_nandop(handle->usb, cs, NAND_READ_TO_RAM, 0) == -1)
744        return -1;
745
746    uint16_t result[4];
747
748    if(usbdev_recvbulk(handle->usb, result, sizeof(result)) == -1)
749        return -1;
750
751    return 0;
752}
753
ingenic.h
1/* This file is based on code by Ingenic Semiconductor Co., Ltd. */
2
3#ifndef __INGENIC__H__
4#define __INGENIC__H__
5
6#include <stdint.h>
7
8#define VR_GET_CPU_INFO 0x00
9#define VR_SET_DATA_ADDRESS 0x01
10#define VR_SET_DATA_LENGTH 0x02
11#define VR_FLUSH_CACHES 0x03
12#define VR_PROGRAM_START1 0x04
13#define VR_PROGRAM_START2 0x05
14#define VR_NOR_OPS 0x06
15#define VR_NAND_OPS 0x07
16#define VR_SDRAM_OPS 0x08
17#define VR_CONFIGRATION 0x09
18#define VR_GET_NUM 0x0a
19
20#define CMDSET_SPL 1
21#define CMDSET_USBBOOT 2
22
23#define INGENIC_STAGE1 1
24#define INGENIC_STAGE2 2
25
26#define STAGE1_DEBUG_BOOT 0
27#define STAGE1_DEBUG_MEMTEST 1
28#define STAGE1_DEBUG_GPIO_SET 2
29#define STAGE1_DEBUG_GPIO_CLEAR 3
30
31#define STAGE1_BASE 0x2000
32#define STAGE2_CODESIZE 0x400000
33#define SDRAM_BASE 0x80000000
34
35#define STAGE2_IOBUF (2048 * 128)
36
37#define DS_flash_info 0
38#define DS_hand 1
39
40#define SDRAM_LOAD 0
41
42#define NAND_QUERY 0
43#define NAND_INIT 1
44#define NAND_MARK_BAD 2
45#define NAND_READ_OOB 3
46#define NAND_READ_RAW 4
47#define NAND_ERASE 5
48#define NAND_READ 6
49#define NAND_PROGRAM 7
50#define NAND_READ_TO_RAM 8
51
52#define OOB_ECC 0
53#define OOB_NO_ECC 1
54#define NO_OOB 2
55#define NAND_RAW (1 << 7)
56
57#define PROGRESS_INIT 0
58#define PROGRESS_UPDATE 1
59#define PROGRESS_FINI 2
60
61typedef struct {
62    /* debug args */
63    uint8_t debug_ops;
64    uint8_t pin_num;
65    uint32_t start;
66    uint32_t size;
67} __attribute__((packed)) ingenic_stage1_debug_t;
68
69typedef struct {
70    /* CPU ID */
71    uint32_t cpu_id;
72
73    /* PLL args */
74    uint8_t ext_clk;
75    uint8_t cpu_speed;
76    uint8_t phm_div;
77    uint8_t use_uart;
78    uint32_t baudrate;
79
80    /* SDRAM args */
81    uint8_t bus_width;
82    uint8_t bank_num;
83    uint8_t row_addr;
84    uint8_t col_addr;
85    uint8_t is_mobile;
86    uint8_t is_busshare;
87
88    ingenic_stage1_debug_t debug;
89} __attribute__((packed)) firmware_config_t;
90
91typedef struct {
92    /* nand flash info */
93    uint32_t cpuid; /* cpu type */
94    uint32_t nand_bw; /* bus width */
95    uint32_t nand_rc; /* row cycle */
96    uint32_t nand_ps; /* page size */
97    uint32_t nand_ppb; /* page number per block */
98    uint32_t nand_force_erase;
99    uint32_t nand_pn; /* page number in total */
100    uint32_t nand_os; /* oob size */
101    uint32_t nand_eccpos;
102    uint32_t nand_bbpage;
103    uint32_t nand_bbpos;
104    uint32_t nand_plane;
105    uint32_t nand_bchbit;
106    uint32_t nand_wppin;
107    uint32_t nand_bpc; /* block number per chip */
108} nand_config_t;
109
110typedef struct {
111    uint8_t vid;
112    uint8_t pid;
113    uint8_t chip;
114    uint8_t page;
115    uint8_t plane;
116} nand_info_t;
117
118
119typedef struct {
120    void (*cmdset_change)(uint32_t cmdset, void *arg);
121    void (*progress)(int action, int value, int max, void *arg);
122} ingenic_callbacks_t;
123
124void *ingenic_open(void *usb_hndl);
125void ingenic_close(void *hndl);
126void ingenic_set_callbacks(void *hndl, const ingenic_callbacks_t *callbacks, void *arg);
127
128int ingenic_redetect(void *hndl);
129int ingenic_cmdset(void *hndl);
130int ingenic_type(void *hndl);
131uint32_t ingenic_sdram_size(void *hndl);
132
133int ingenic_rebuild(void *hndl);
134int ingenic_loadstage(void *hndl, int id, const char *filename);
135int ingenic_stage1_debugop(void *device, const char *filename, uint32_t op, uint32_t pin, uint32_t base, uint32_t size);
136int ingenic_memtest(void *hndl, const char *filename, uint32_t base, uint32_t size, uint32_t *fail);
137
138int ingenic_configure_stage2(void *hndl);
139int ingenic_load_sdram(void *hndl, void *data, uint32_t base, uint32_t size);
140int ingenic_load_sdram_file(void *hndl, uint32_t base, const char *filename);
141int ingenic_go(void *hndl, uint32_t address);
142
143int ingenic_query_nand(void *hndl, int cs, nand_info_t *info);
144int ingenic_dump_nand(void *hndl, int cs, int start, int pages, int type, const char *filename);
145int ingenic_program_nand(void *hndl, int cs, int start, int type, const char *filename);
146int ingenic_erase_nand(void *hndl, int cs, int start, int blocks);
147int ingenic_load_nand(void *hndl, int cs, int start, int pages, uint32_t base);
148
149#endif
initial.cfg
1# Configuration variables:
2# STAGE1_FILE, STAGE2_FILE
3
4set STAGE1_FILE fw.bin
5set STAGE2_FILE usb_boot.bin
6
7set EXTCLK 12 # Define the external crystal in MHz
8set CPUSPEED 252 # Define the PLL output frequency
9set PHMDIV 3 # Define the frequency divider ratio of PLL=CCLK:PCLK=HCLK=MCLK
10set BAUDRATE 57600 # Define the uart baudrate
11set USEUART 0 # UART number
12
13set SDRAM_BUSWIDTH 16 # The bus width of the SDRAM in bits (16|32)
14set SDRAM_BANKS 4 # The bank number (2|4)
15set SDRAM_ROWADDR 13 # Row address width in bits (11-13)
16set SDRAM_COLADDR 9 # Column address width in bits (8-12)
17set SDRAM_ISMOBILE 0 # Define whether SDRAM is mobile SDRAM (only or Jz4750), 1: yes 0: no
18set SDRAM_ISBUSSHARE 1 # Define whether SDRAM bus share with NAND 1:shared 0:unshared
19
20set NAND_BUSWIDTH 8 # The width of the NAND flash chip in bits (8|16|32)
21set NAND_ROWCYCLES 3 # The row address cycles (2|3)
22set NAND_PAGESIZE 2048 # The page size of the NAND chip in bytes(512|2048|4096)
23set NAND_PAGEPERBLOCK 128 # The page number per block
24set NAND_FORCEERASE 1 # The force to erase flag (0|1)
25set NAND_OOBSIZE 64 # OOB size in byte
26set NAND_ECCPOS 8 # Specify the ECC offset inside the oob data (0-[oobsize-1])
27set NAND_BADBLOCKPOS 0 # Specify the badblock flag offset inside the oob (0-[oobsize-1])
28set NAND_BADBLOCKPAGE 0 # Specify the page number of badblock flag inside a block(0-[PAGEPERBLOCK-1])
29set NAND_PLANENUM 1 # The planes number of target nand flash
30set NAND_BCHBIT 8 # Specify the hardware BCH algorithm for 4750 (4|8)
31set NAND_WPPIN 0 # Specify the write protect pin number
32set NAND_BLOCKPERCHIP 4096 # Specify the block number per chip,0 means ignore
33
34rebuildcfg
main.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include "usbdev.h"
23#include "debug.h"
24#include "devmgr.h"
25#include "ingenic.h"
26#include "shell.h"
27
28static void usage(const char *app) {
29    printf(
30        "Usage: \n"
31        " Enumeration: %1$s -e\n"
32                " Interactive mode: %1$s -i <INDEX>\n"
33                " Batch mode (with script): %1$s -i <INDEX> -b <FILE>\n"
34                " Batch mode (command list): %1$s -i <INDEX> -c <COMMAND>\n\n"
35
36        "USB loader tool for Ingenic Semiconductor XBurst-based SoC\n\n"
37        " -e Enumerate devices only\n"
38        " -i <INDEX> Open device with index INDEX in interactive or batch mode\n"
39        " -c <CMD> Run semicolon-separated commands and exit\n"
40                " -d <DEBUG> Set output level (0 - no reporting, 4 - max reporting), default = 1 (errors only)\n"
41                " -C <FILE> Execute configuration script FILE before anything else\n"
42        " -b <FILE> Execute script in FILE\n\n", app);
43}
44
45static void dev_handler(int idx, uint16_t vid, uint16_t pid, void *data) {
46    printf(" Device %d: %04hX:%04hX\n", idx, vid, pid);
47}
48
49int main(int argc, char *argv[]) {
50    int ch;
51    int idx = -1, enumerate = 0;
52    char *cmd = NULL, *script = NULL, *config = NULL;
53
54    while((ch = getopt(argc, argv, "b:i:ec:d:C:")) != -1) {
55        switch(ch) {
56        case 'e':
57            enumerate = 1;
58
59            break;
60
61        case 'i':
62            idx = atoi(optarg);
63
64            break;
65
66        case 'c':
67            cmd = optarg;
68
69            break;
70
71        case 'b':
72            script = optarg;
73
74            break;
75
76        case 'C':
77            config = optarg;
78
79            break;
80
81        case 'd':
82            set_debug_level(atoi(optarg));
83
84            break;
85
86        default:
87            usage(argv[0]);
88
89            return 1;
90        }
91    }
92
93    if(!enumerate && idx < 0) {
94        usage(argv[0]);
95
96        return 1;
97    }
98
99    if(usbdev_init() == -1) {
100        perror("usbdev_init");
101
102        return 1;
103    }
104
105    atexit(usbdev_fini);
106
107    if(usbdev_enumerate() == -1) {
108        perror("usbdev_enumerate");
109
110        return 1;
111    }
112
113    if(enumerate) {
114        printf("Ingenic devices list:\n");
115
116        enum_devices(dev_handler);
117
118        return 0;
119    }
120
121    void *data = get_device(idx);
122
123    if(data == NULL) {
124        fprintf(stderr, "Device with index %d not found\n", idx);
125
126        return 1;
127    }
128
129    void *hndl = usbdev_open(data);
130
131    if(hndl == NULL) {
132        perror("usbdev_open");
133
134        return 1;
135    }
136
137    int ret = 0;
138
139    void *ingenic = ingenic_open(hndl);
140
141    if(ingenic == NULL) {
142        perror("ingenic_open");
143
144        ret = 1;
145
146        goto exit_usb;
147    }
148
149    shell_context_t *shell = shell_init(ingenic);
150
151    if(shell == NULL) {
152        perror("shell_init");
153
154        ret = 1;
155
156        goto exit_ingenic;
157    }
158
159    if(config) {
160        if(shell_source(shell, config) == -1) {
161            perror("shell_source");
162
163            ret = 1;
164
165            goto exit_shell;
166        }
167    }
168
169    if(cmd != NULL) {
170        if(shell_execute(shell, cmd) == -1) {
171            perror("shell_execute");
172
173            ret = 1;
174        }
175
176    } else if(script != NULL) {
177        if(shell_source(shell, script) == -1) {
178            perror("shell_source");
179
180            ret = 1;
181        }
182    } else
183        shell_interactive(shell);
184
185exit_shell:
186    shell_fini(shell);
187
188exit_ingenic:
189    ingenic_close(ingenic);
190
191exit_usb:
192    usbdev_close(hndl);
193
194    return ret;
195}
script/dump_minios.scr
1echo "Dumping script"
2nquery 0
3
4#set NAND_RAW 1
5set NAND_ECCPOS 3
6rebuildcfg
7boot
8
9echo "Configured for bootloader IO!"
10
11ndump_oob 0 0 128 "dump/nand.bin"
12
13set NAND_RAW
14set NAND_ECCPOS 8
15rebuildcfg
16boot
17
18echo "Configuration restored"
19
20ndump 0 128 66 "dump/loader.bin"
21ndump 0 256 1 "dump/def_boot.bin"
22ndump 0 512 3061 "dump/img_boot.bin"
23ndump 0 8192 8192 "dump/minios.bin"
24ndump 0 32768 32768 "dump/res.bin"
25
shell.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#ifdef WITH_READLINE
23#include <readline/readline.h>
24#include <readline/history.h>
25#endif
26#include <stdlib.h>
27#include <unistd.h>
28#include <errno.h>
29#include <sys/ioctl.h>
30
31#include "shell_internal.h"
32#include "debug.h"
33#include "ingenic.h"
34
35static void shell_update_cmdset(uint32_t cmdset, void *arg);
36static void shell_progress(int action, int value, int max, void *arg);
37
38static const ingenic_callbacks_t shell_callbacks = {
39    shell_update_cmdset,
40    shell_progress
41};
42
43static const struct {
44    int set;
45    const char *name;
46    const shell_command_t *commands;
47} cmdsets[] = {
48    { CMDSET_SPL, "SPL", spl_cmdset },
49    { CMDSET_USBBOOT, "USBBoot", usbboot_cmdset },
50    { 0, NULL, NULL }
51};
52
53shell_context_t *shell_init(void *ingenic) {
54#ifdef WITH_READLINE
55    rl_initialize();
56#endif
57
58    debug(LEVEL_DEBUG, "Initializing shell\n");
59
60    shell_context_t *ctx = malloc(sizeof(shell_context_t));
61    memset(ctx, 0, sizeof(shell_context_t));
62    ctx->device = ingenic;
63
64    ingenic_set_callbacks(ingenic, &shell_callbacks, ctx);
65
66    shell_update_cmdset(ingenic_cmdset(ingenic), ctx);
67
68    return ctx;
69}
70
71int shell_enumerate_commands(shell_context_t *ctx, int (*callback)(shell_context_t *ctx, const shell_command_t *cmd, void *arg), void *arg) {
72    for(int i = 0; builtin_cmdset[i].cmd != NULL; i++) {
73        int ret = callback(ctx, builtin_cmdset + i, arg);
74
75        if(ret != 0)
76            return ret;
77    }
78
79    if(ctx->set_cmds)
80        for(int i = 0; ctx->set_cmds[i].cmd != NULL; i++) {
81            int ret = callback(ctx, ctx->set_cmds + i, arg);
82
83            if(ret != 0)
84                return ret;
85        }
86
87    return 0;
88}
89
90static int shell_run_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) {
91    shell_run_data_t *data = arg;
92
93    if(strcmp(cmd->cmd, data->argv[0]) == 0) {
94        int invalid = 0;
95
96        if(cmd->args == NULL && data->argc != 1)
97            invalid = 1;
98        else if(cmd->args) {
99            char *dup = strdup(cmd->args), *save = dup, *ptrptr = NULL;
100
101            int pos = 1;
102            int max_tokens = 1;
103
104            for(char *token = strtok_r(dup, " ", &ptrptr); token; token = strtok_r(NULL, " ", &ptrptr)) {
105                if(strcmp(token, "...") == 0) {
106                    max_tokens = -1;
107
108                    break;
109                }
110
111                max_tokens++;
112
113                if(data->argc - 1 < pos) {
114                    if(*token == '[') {
115                        break;
116                    } else if(*token == '<') {
117                        invalid = 1;
118
119                        break;
120                    }
121                }
122
123                pos++;
124            }
125
126            if(max_tokens != -1 && data->argc > max_tokens)
127                invalid = 1;
128
129
130            free(save);
131        }
132
133        if(invalid) {
134            if(cmd->args)
135                fprintf(stderr, "Usage: %s %s\n", cmd->cmd, cmd->args);
136            else
137                fprintf(stderr, "Usage: %s\n", cmd->cmd);
138
139            errno = EINVAL;
140
141            return -1;
142
143        } else {
144            int ret = cmd->handler(ctx, data->argc, data->argv);
145
146            if(ret == 0)
147                return 1;
148            else
149                return ret;
150        }
151    } else
152        return 0;
153}
154
155int shell_run(shell_context_t *ctx, int argc, char *argv[]) {
156    shell_run_data_t data = { argc, argv };
157
158    int ret = shell_enumerate_commands(ctx, shell_run_function, &data);
159
160    if(ret == 0) {
161        debug(LEVEL_ERROR, "Bad command '%s'\n", argv[0]);
162
163        errno = EINVAL;
164        return -1;
165
166    } else if(ret == 1) {
167        return 0;
168    } else
169        return ret;
170}
171
172int shell_execute(shell_context_t *ctx, const char *cmd) {
173    yyscan_t scanner;
174    if(yylex_init_extra(ctx, &scanner) == -1)
175        return -1;
176
177    ctx->line = strdup(cmd);
178    char *ptr = ctx->line;
179
180    int token;
181    int state = STATE_WANTSTR;
182    int argc = 0;
183    char **argv = NULL;
184    int fret = 0;
185
186    do {
187        int noway = 0;
188
189        token = yylex(scanner);
190
191        if((token == TOK_SEPARATOR || token == TOK_COMMENT || token == 0)) {
192            if(argc > 0) {
193                int ret = shell_run(ctx, argc, argv);
194
195                for(int i = 0; i < argc; i++) {
196                    free(argv[i]);
197                }
198
199                free(argv);
200
201                argv = NULL;
202                argc = 0;
203
204                if(ret == -1) {
205                    fret = -1;
206
207                    break;
208                }
209            }
210
211            state = STATE_WANTSTR;
212        } else {
213            switch(state) {
214            case STATE_WANTSTR:
215                if(token == TOK_SPACE) {
216                    state = STATE_WANTSTR;
217                } else if(token == TOK_STRING) {
218                    int oargc = argc++;
219
220                    argv = realloc(argv, sizeof(char *) * argc);
221
222                    argv[oargc] = ctx->strval;
223
224                    state = STATE_WANTSPACE;
225                } else {
226                    noway = 1;
227                }
228
229                break;
230
231            case STATE_WANTSPACE:
232                if(token == TOK_STRING) {
233                    free(ctx->strval);
234
235                    noway = 1;
236                } else if(token == TOK_SPACE) {
237                    state = STATE_WANTSTR;
238                } else
239                    noway = 1;
240            }
241
242            if(noway) {
243                debug(LEVEL_ERROR, "No way from state %d by token %d\n", state, token);
244
245                for(int i = 0; i < argc; i++)
246                    free(argv[i]);
247
248                free(argv);
249
250                fret = -1;
251
252                break;
253            }
254        }
255
256    } while(token && token != TOK_COMMENT);
257
258    free(ptr);
259
260    yylex_destroy(scanner);
261
262    return fret;
263}
264
265int shell_pull(shell_context_t *ctx, char *buf, int maxlen) {
266    size_t len = strlen(ctx->line);
267
268    if(len < maxlen)
269        maxlen = len;
270
271    memcpy(buf, ctx->line, maxlen);
272
273    ctx->line += maxlen;
274
275    return maxlen;
276}
277
278void shell_fini(shell_context_t *ctx) {
279    free(ctx);
280}
281
282int shell_source(shell_context_t *ctx, const char *filename) {
283    ctx->shell_exit = 0;
284
285    FILE *file = fopen(filename, "r");
286
287    if(file == NULL) {
288        return -1;
289    }
290
291    char *line;
292
293    while((line = fgets(ctx->linebuf, sizeof(ctx->linebuf), file)) && !ctx->shell_exit) {
294        if(shell_execute(ctx, line) == -1) {
295            fclose(file);
296
297            return -1;
298        }
299    }
300
301    fclose(file);
302
303    return 0;
304}
305
306#ifdef WITH_READLINE
307static shell_context_t *completion_context;
308static char **completion_matches;
309static int completion_matches_count = 0;
310
311static int shell_completion_filler(shell_context_t *ctx, const shell_command_t *cmd, void *arg) {
312    const char *part = arg;
313
314    size_t len = strlen(part), cmdlen = strlen(cmd->cmd);
315
316    if(cmdlen >= len && memcmp(part, cmd->cmd, len) == 0) {
317        int idx = completion_matches_count++;
318
319        completion_matches = realloc(completion_matches, sizeof(char **) * completion_matches_count);
320        completion_matches[idx] = strdup(cmd->cmd);
321    }
322
323    return 0;
324}
325
326static char *shell_completion(const char *partial, int state) {
327    static int completion_pass = 0, completion_matches_offset = 0;
328
329    if(state == 0) {
330        if(completion_matches) {
331            for(int i = 0; i < completion_matches_count; i++)
332                if(completion_matches[i])
333                    free(completion_matches[i]);
334
335            free(completion_matches);
336
337            completion_matches = NULL;
338            completion_matches_count = 0;
339        }
340
341        completion_pass = 0;
342
343        char *tmp = rl_line_buffer;
344
345        while(isspace(*tmp)) tmp++;
346
347        int not_first = 0;
348
349        for(; *tmp; tmp++) {
350            for(const char *sep = rl_basic_word_break_characters; *sep; sep++) {
351                if(*tmp == *sep) {
352                    not_first = 1;
353
354                    break;
355                }
356            }
357
358            if(not_first)
359                break;
360        }
361
362        if(not_first) {
363            completion_pass = 1;
364
365            return rl_filename_completion_function(partial, state);
366        } else {
367            shell_enumerate_commands(completion_context, shell_completion_filler, (void *) partial);
368
369            completion_matches_offset = 0;
370        }
371    }
372
373    if(completion_pass) {
374        return rl_filename_completion_function(partial, state);
375
376    } else if(completion_matches_offset == completion_matches_count) {
377        return NULL;
378    } else {
379        char *val = completion_matches[completion_matches_offset];
380
381        completion_matches[completion_matches_offset++] = NULL;
382
383        return val;
384    }
385}
386#endif
387
388void shell_interactive(shell_context_t *ctx) {
389    ctx->shell_exit = 0;
390
391#ifndef WITH_READLINE
392    char *line;
393
394    while(!ctx->shell_exit) {
395        fputs("jzboot> ", stdout);
396        fflush(stdout);
397
398        line = fgets(ctx->linebuf, sizeof(ctx->linebuf), stdin);
399
400        if(line == NULL)
401            break;
402
403        shell_execute(ctx, line);
404    }
405#else
406    rl_completion_entry_function = shell_completion;
407    completion_context = ctx;
408
409    rl_set_signals();
410
411    rl_filename_quote_characters = "\" ";
412    while(!ctx->shell_exit) {
413        char *line = readline("jzboot> ");
414
415        if(line == NULL) {
416            break;
417        }
418
419        add_history(line);
420
421        shell_execute(ctx, line);
422
423        free(line);
424    }
425
426    rl_clear_signals();
427#endif
428}
429
430static void shell_update_cmdset(uint32_t cmdset, void *arg) {
431    shell_context_t *ctx = arg;
432
433    ctx->set_cmds = NULL;
434
435    for(int i = 0; cmdsets[i].name != NULL; i++) {
436        if(cmdsets[i].set == cmdset) {
437            printf("Shell: using command set '%s', run 'help' for command list. CPU: %04X\n", cmdsets[i].name, ingenic_type(ctx->device));
438
439            ctx->set_cmds = cmdsets[i].commands;
440
441            return;
442        }
443    }
444
445    debug(LEVEL_ERROR, "Shell: unknown cmdset %u\n", cmdset);
446}
447
448void *shell_device(shell_context_t *ctx) {
449    return ctx->device;
450}
451
452void shell_exit(shell_context_t *ctx, int val) {
453    ctx->shell_exit = val;
454}
455
456static void shell_progress(int action, int value, int max, void *arg) {
457    shell_context_t *ctx = arg;
458
459    if(isatty(STDOUT_FILENO)) {
460        struct winsize size;
461
462        int progress, percent;
463
464        if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == -1)
465            return;
466
467        int bar_size = size.ws_col - 6;
468
469        switch(action) {
470        case PROGRESS_INIT:
471            ctx->prev_progress = -1;
472
473
474        case PROGRESS_FINI:
475            putchar('\r');
476
477            for(int i = 0; i < size.ws_col; i++)
478                putchar(' ');
479
480            putchar('\r');
481
482            fflush(stdout);
483
484            break;
485
486        case PROGRESS_UPDATE:
487            progress = value * bar_size / max;
488            percent = value * 100 / max;
489
490            if(progress != ctx->prev_progress) {
491                fputs("\r|", stdout);
492
493                for(int i = 0; i < progress; i++) {
494                    putchar('=');
495                }
496
497                for(int i = progress; i < bar_size; i++)
498                    putchar(' ');
499
500                printf("|%3d%%", percent);
501
502                fflush(stdout);
503
504                ctx->prev_progress = progress;
505
506            }
507
508
509            break;
510
511        }
512    }
513}
514
shell.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __SHELL__H__
20#define __SHELL__H__
21
22#ifndef SHELL_INTERNALS
23typedef void shell_context_t;
24#endif
25
26typedef struct shell_command {
27    const char *cmd;
28    const char *description;
29    int (*handler)(shell_context_t *ctx, int argc, char *argv[]);
30    const char *args;
31} shell_command_t;
32
33shell_context_t *shell_init(void *ingenic);
34void shell_fini(shell_context_t *context);
35
36void shell_interactive(shell_context_t *ctx);
37int shell_source(shell_context_t *ctx, const char *filename);
38int shell_execute(shell_context_t *ctx, const char *input);
39void *shell_device(shell_context_t *ctx);
40int shell_run(shell_context_t *ctx, int argc, char *argv[]);
41
42void shell_exit(shell_context_t *ctx, int val);
43int shell_enumerate_commands(shell_context_t *ctx, int (*callback)(shell_context_t *ctx, const shell_command_t *cmd, void *arg), void *arg);
44
45extern const shell_command_t spl_cmdset[];
46extern const shell_command_t usbboot_cmdset[];
47extern const shell_command_t builtin_cmdset[];
48
49#endif
shell_builtins.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 <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <errno.h>
25
26#include "shell.h"
27#include "config.h"
28#include "ingenic.h"
29
30static int builtin_help(shell_context_t *ctx, int argc, char *argv[]);
31static int builtin_exit(shell_context_t *ctx, int argc, char *argv[]);
32static int builtin_source(shell_context_t *ctx, int argc, char *argv[]);
33static int builtin_echo(shell_context_t *ctx, int argc, char *argv[]);
34static int builtin_sleep(shell_context_t *ctx, int argc, char *argv[]);
35static int builtin_redetect(shell_context_t *ctx, int argc, char *argv[]);
36static int builtin_rebuildcfg(shell_context_t *ctx, int argc, char *argv[]);
37static int builtin_set(shell_context_t *ctx, int argc, char *argv[]);
38static int builtin_safe(shell_context_t *ctx, int argc, char *argv[]);
39
40const shell_command_t builtin_cmdset[] = {
41    { "help", "Display this message", builtin_help, NULL },
42    { "exit", "Batch: stop current script, interactive: end session", builtin_exit, NULL },
43    { "source", "Run specified script", builtin_source, "<FILENAME>" },
44    { "echo", "Output specified string", builtin_echo, "<STRING> ..." },
45    { "sleep", "Sleep a specified amount of time", builtin_sleep, "<MILLISECONDS>" },
46    { "set", "Print or set configuraton variables", builtin_set, "[VARIABLE] [VALUE]" },
47    { "safe", "Run command ignoring errors", builtin_safe, "<COMMAND> [ARG] ..." },
48
49    { "redetect", "Redetect CPU", builtin_redetect, NULL },
50    { "rebuildcfg", "Rebuild firmware configuration data", builtin_rebuildcfg, NULL },
51
52    { NULL, NULL, NULL }
53};
54
55static int help_maxwidth_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) {
56    int len = strlen(cmd->cmd), *maxlen = arg;
57
58    if(cmd->args)
59        len += strlen(cmd->args) + 1;
60
61    if(len > *maxlen)
62        *maxlen = len;
63
64    return 0;
65}
66
67static int help_print_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) {
68    int len = strlen(cmd->cmd), *maxlen = arg;
69
70    fputs(cmd->cmd, stdout);
71
72    if(cmd->args) {
73        len += strlen(cmd->args) + 1;
74
75        putchar(' ');
76        fputs(cmd->args, stdout);
77    }
78
79    for(int i = 0; i < *maxlen - len; i++)
80        putchar(' ');
81
82    puts(cmd->description);
83
84    return 0;
85}
86
87static int builtin_help(shell_context_t *ctx, int argc, char *argv[]) {
88    int maxwidth = 0;
89
90    shell_enumerate_commands(ctx, help_maxwidth_function, &maxwidth);
91
92    maxwidth += 2;
93
94    return shell_enumerate_commands(ctx, help_print_function, &maxwidth);
95}
96
97static int builtin_exit(shell_context_t *ctx, int argc, char *argv[]) {
98    shell_exit(ctx, 1);
99
100    return 0;
101}
102
103static int builtin_source(shell_context_t *ctx, int argc, char *argv[]) {
104    int ret = shell_source(ctx, argv[1]);
105
106    if(ret == -1) {
107        fprintf(stderr, "Error while sourcing file %s: %s\n", argv[1], strerror(errno));
108    }
109
110    shell_exit(ctx, 0);
111
112    return ret;
113}
114
115static int builtin_echo(shell_context_t *ctx, int argc, char *argv[]) {
116    for(int i = 1; i < argc; i++) {
117        fputs(argv[i], stdout);
118
119        putchar((i < argc - 1) ? ' ' : '\n');
120    }
121
122    return 0;
123}
124
125static int builtin_sleep(shell_context_t *ctx, int argc, char *argv[]) {
126    uint32_t ms = atoi(argv[1]);
127
128    usleep(ms * 1000);
129
130    return 0;
131}
132
133static int builtin_redetect(shell_context_t *ctx, int argc, char *argv[]) {
134    if(ingenic_redetect(shell_device(ctx)) == -1) {
135        perror("ingenic_redetect");
136
137        return -1;
138    } else
139        return 0;
140}
141
142
143static int builtin_set(shell_context_t *ctx, int argc, char *argv[]) {
144    if(argc == 1) {
145        if(cfg_environ) {
146            for(int i = 0; cfg_environ[i] != NULL; i++)
147                printf("%s\n", cfg_environ[i]);
148        }
149
150    } else if(argc == 2) {
151        cfg_unsetenv(argv[1]);
152
153    } else if(argc == 3) {
154        cfg_setenv(argv[1], argv[2]);
155    }
156
157    return 0;
158}
159
160
161static int builtin_rebuildcfg(shell_context_t *ctx, int argc, char *argv[]) {
162    return ingenic_rebuild(shell_device(ctx));
163}
164
165static int builtin_safe(shell_context_t *ctx, int argc, char *argv[]) {
166    if(shell_run(ctx, argc - 1, argv + 1) == -1)
167        perror("shell_run");
168
169    return 0;
170}
171
shell_internal.h
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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#ifndef __SHELL_INTERNAL__H__
20#define __SHELL_INTERNAL__H__
21
22#define SHELL_INTERNALS
23#define STATE_WANTSTR 0
24#define STATE_WANTSPACE 1
25
26#define TOK_SEPARATOR 1
27#define TOK_STRING 2
28#define TOK_SPACE 3
29#define TOK_COMMENT 4
30
31typedef struct {
32    void *device;
33    char linebuf[512];
34    char *strval;
35    char *line;
36    const struct shell_command *set_cmds;
37    int shell_exit;
38    int prev_progress;
39} shell_context_t;
40
41
42typedef struct {
43    int argc;
44    char **argv;
45} shell_run_data_t;
46
47int shell_pull(shell_context_t *ctx, char *buf, int maxlen);
48
49#ifndef FLEX_SCANNER
50typedef void *yyscan_t;
51int yylex_init(yyscan_t *ptr_yy_globals);
52int yylex_init_extra(shell_context_t *user_defined, yyscan_t *ptr_yy_globals);
53int yylex(yyscan_t yyscanner) ;
54int yylex_destroy (yyscan_t yyscanner) ;
55#else
56#define YY_EXTRA_TYPE shell_context_t *
57#define YY_INPUT(buf, result, max_size) result = shell_pull(yyextra, buf, max_size);
58#endif
59
60#include "shell.h"
61
62#endif
63
shell_lex.c
1#line 2 "shell_lex.c"
2
3#line 4 "shell_lex.c"
4
5#define YY_INT_ALIGNED short int
6
7/* A lexical scanner generated by flex */
8
9#define FLEX_SCANNER
10#define YY_FLEX_MAJOR_VERSION 2
11#define YY_FLEX_MINOR_VERSION 5
12#define YY_FLEX_SUBMINOR_VERSION 35
13#if YY_FLEX_SUBMINOR_VERSION > 0
14#define FLEX_BETA
15#endif
16
17/* First, we deal with platform-specific or compiler-specific issues. */
18
19/* begin standard C headers. */
20#include <stdio.h>
21#include <string.h>
22#include <errno.h>
23#include <stdlib.h>
24
25/* end standard C headers. */
26
27/* flex integer type definitions */
28
29#ifndef FLEXINT_H
30#define FLEXINT_H
31
32/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */
33
34#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
35
36/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
37 * if you want the limit (max/min) macros for int types.
38 */
39#ifndef __STDC_LIMIT_MACROS
40#define __STDC_LIMIT_MACROS 1
41#endif
42
43#include <inttypes.h>
44typedef int8_t flex_int8_t;
45typedef uint8_t flex_uint8_t;
46typedef int16_t flex_int16_t;
47typedef uint16_t flex_uint16_t;
48typedef int32_t flex_int32_t;
49typedef uint32_t flex_uint32_t;
50#else
51typedef signed char flex_int8_t;
52typedef short int flex_int16_t;
53typedef int flex_int32_t;
54typedef unsigned char flex_uint8_t;
55typedef unsigned short int flex_uint16_t;
56typedef unsigned int flex_uint32_t;
57
58/* Limits of integral types. */
59#ifndef INT8_MIN
60#define INT8_MIN (-128)
61#endif
62#ifndef INT16_MIN
63#define INT16_MIN (-32767-1)
64#endif
65#ifndef INT32_MIN
66#define INT32_MIN (-2147483647-1)
67#endif
68#ifndef INT8_MAX
69#define INT8_MAX (127)
70#endif
71#ifndef INT16_MAX
72#define INT16_MAX (32767)
73#endif
74#ifndef INT32_MAX
75#define INT32_MAX (2147483647)
76#endif
77#ifndef UINT8_MAX
78#define UINT8_MAX (255U)
79#endif
80#ifndef UINT16_MAX
81#define UINT16_MAX (65535U)
82#endif
83#ifndef UINT32_MAX
84#define UINT32_MAX (4294967295U)
85#endif
86
87#endif /* ! C99 */
88
89#endif /* ! FLEXINT_H */
90
91#ifdef __cplusplus
92
93/* The "const" storage-class-modifier is valid. */
94#define YY_USE_CONST
95
96#else /* ! __cplusplus */
97
98/* C99 requires __STDC__ to be defined as 1. */
99#if defined (__STDC__)
100
101#define YY_USE_CONST
102
103#endif /* defined (__STDC__) */
104#endif /* ! __cplusplus */
105
106#ifdef YY_USE_CONST
107#define yyconst const
108#else
109#define yyconst
110#endif
111
112/* Returned upon end-of-file. */
113#define YY_NULL 0
114
115/* Promotes a possibly negative, possibly signed char to an unsigned
116 * integer for use as an array index. If the signed char is negative,
117 * we want to instead treat it as an 8-bit unsigned char, hence the
118 * double cast.
119 */
120#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
121
122/* An opaque pointer. */
123#ifndef YY_TYPEDEF_YY_SCANNER_T
124#define YY_TYPEDEF_YY_SCANNER_T
125typedef void* yyscan_t;
126#endif
127
128/* For convenience, these vars (plus the bison vars far below)
129   are macros in the reentrant scanner. */
130#define yyin yyg->yyin_r
131#define yyout yyg->yyout_r
132#define yyextra yyg->yyextra_r
133#define yyleng yyg->yyleng_r
134#define yytext yyg->yytext_r
135#define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno)
136#define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column)
137#define yy_flex_debug yyg->yy_flex_debug_r
138
139/* Enter a start condition. This macro really ought to take a parameter,
140 * but we do it the disgusting crufty way forced on us by the ()-less
141 * definition of BEGIN.
142 */
143#define BEGIN yyg->yy_start = 1 + 2 *
144
145/* Translate the current start state into a value that can be later handed
146 * to BEGIN to return to the state. The YYSTATE alias is for lex
147 * compatibility.
148 */
149#define YY_START ((yyg->yy_start - 1) / 2)
150#define YYSTATE YY_START
151
152/* Action number for EOF rule of a given start state. */
153#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
154
155/* Special action meaning "start processing a new file". */
156#define YY_NEW_FILE yyrestart(yyin ,yyscanner )
157
158#define YY_END_OF_BUFFER_CHAR 0
159
160/* Size of default input buffer. */
161#ifndef YY_BUF_SIZE
162#ifdef __ia64__
163/* On IA-64, the buffer size is 16k, not 8k.
164 * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
165 * Ditto for the __ia64__ case accordingly.
166 */
167#define YY_BUF_SIZE 32768
168#else
169#define YY_BUF_SIZE 16384
170#endif /* __ia64__ */
171#endif
172
173/* The state buf must be large enough to hold one state per character in the main buffer.
174 */
175#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type))
176
177#ifndef YY_TYPEDEF_YY_BUFFER_STATE
178#define YY_TYPEDEF_YY_BUFFER_STATE
179typedef struct yy_buffer_state *YY_BUFFER_STATE;
180#endif
181
182#define EOB_ACT_CONTINUE_SCAN 0
183#define EOB_ACT_END_OF_FILE 1
184#define EOB_ACT_LAST_MATCH 2
185
186    #define YY_LESS_LINENO(n)
187
188/* Return all but the first "n" matched characters back to the input stream. */
189#define yyless(n) \
190    do \
191        { \
192        /* Undo effects of setting up yytext. */ \
193        int yyless_macro_arg = (n); \
194        YY_LESS_LINENO(yyless_macro_arg);\
195        *yy_cp = yyg->yy_hold_char; \
196        YY_RESTORE_YY_MORE_OFFSET \
197        yyg->yy_c_buf_p = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \
198        YY_DO_BEFORE_ACTION; /* set up yytext again */ \
199        } \
200    while ( 0 )
201
202#define unput(c) yyunput( c, yyg->yytext_ptr , yyscanner )
203
204#ifndef YY_TYPEDEF_YY_SIZE_T
205#define YY_TYPEDEF_YY_SIZE_T
206typedef size_t yy_size_t;
207#endif
208
209#ifndef YY_STRUCT_YY_BUFFER_STATE
210#define YY_STRUCT_YY_BUFFER_STATE
211struct yy_buffer_state
212    {
213    FILE *yy_input_file;
214
215    char *yy_ch_buf; /* input buffer */
216    char *yy_buf_pos; /* current position in input buffer */
217
218    /* Size of input buffer in bytes, not including room for EOB
219     * characters.
220     */
221    yy_size_t yy_buf_size;
222
223    /* Number of characters read into yy_ch_buf, not including EOB
224     * characters.
225     */
226    int yy_n_chars;
227
228    /* Whether we "own" the buffer - i.e., we know we created it,
229     * and can realloc() it to grow it, and should free() it to
230     * delete it.
231     */
232    int yy_is_our_buffer;
233
234    /* Whether this is an "interactive" input source; if so, and
235     * if we're using stdio for input, then we want to use getc()
236     * instead of fread(), to make sure we stop fetching input after
237     * each newline.
238     */
239    int yy_is_interactive;
240
241    /* Whether we're considered to be at the beginning of a line.
242     * If so, '^' rules will be active on the next match, otherwise
243     * not.
244     */
245    int yy_at_bol;
246
247    int yy_bs_lineno; /**< The line count. */
248    int yy_bs_column; /**< The column count. */
249
250    /* Whether to try to fill the input buffer when we reach the
251     * end of it.
252     */
253    int yy_fill_buffer;
254
255    int yy_buffer_status;
256
257#define YY_BUFFER_NEW 0
258#define YY_BUFFER_NORMAL 1
259    /* When an EOF's been seen but there's still some text to process
260     * then we mark the buffer as YY_EOF_PENDING, to indicate that we
261     * shouldn't try reading from the input source any more. We might
262     * still have a bunch of tokens to match, though, because of
263     * possible backing-up.
264     *
265     * When we actually see the EOF, we change the status to "new"
266     * (via yyrestart()), so that the user can continue scanning by
267     * just pointing yyin at a new input file.
268     */
269#define YY_BUFFER_EOF_PENDING 2
270
271    };
272#endif /* !YY_STRUCT_YY_BUFFER_STATE */
273
274/* We provide macros for accessing buffer states in case in the
275 * future we want to put the buffer states in a more general
276 * "scanner state".
277 *
278 * Returns the top of the stack, or NULL.
279 */
280#define YY_CURRENT_BUFFER ( yyg->yy_buffer_stack \
281                          ? yyg->yy_buffer_stack[yyg->yy_buffer_stack_top] \
282                          : NULL)
283
284/* Same as previous macro, but useful when we know that the buffer stack is not
285 * NULL or when we need an lvalue. For internal use only.
286 */
287#define YY_CURRENT_BUFFER_LVALUE yyg->yy_buffer_stack[yyg->yy_buffer_stack_top]
288
289void yyrestart (FILE *input_file ,yyscan_t yyscanner );
290void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
291YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ,yyscan_t yyscanner );
292void yy_delete_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
293void yy_flush_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
294void yypush_buffer_state (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
295void yypop_buffer_state (yyscan_t yyscanner );
296
297static void yyensure_buffer_stack (yyscan_t yyscanner );
298static void yy_load_buffer_state (yyscan_t yyscanner );
299static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ,yyscan_t yyscanner );
300
301#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ,yyscanner)
302
303YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner );
304YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner );
305YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ,yyscan_t yyscanner );
306
307void *yyalloc (yy_size_t ,yyscan_t yyscanner );
308void *yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
309void yyfree (void * ,yyscan_t yyscanner );
310
311#define yy_new_buffer yy_create_buffer
312
313#define yy_set_interactive(is_interactive) \
314    { \
315    if ( ! YY_CURRENT_BUFFER ){ \
316        yyensure_buffer_stack (yyscanner); \
317        YY_CURRENT_BUFFER_LVALUE = \
318            yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); \
319    } \
320    YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \
321    }
322
323#define yy_set_bol(at_bol) \
324    { \
325    if ( ! YY_CURRENT_BUFFER ){\
326        yyensure_buffer_stack (yyscanner); \
327        YY_CURRENT_BUFFER_LVALUE = \
328            yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner); \
329    } \
330    YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \
331    }
332
333#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)
334
335#define yywrap(n) 1
336#define YY_SKIP_YYWRAP
337
338typedef unsigned char YY_CHAR;
339
340typedef int yy_state_type;
341
342#define yytext_ptr yytext_r
343
344static yy_state_type yy_get_previous_state (yyscan_t yyscanner );
345static yy_state_type yy_try_NUL_trans (yy_state_type current_state ,yyscan_t yyscanner);
346static int yy_get_next_buffer (yyscan_t yyscanner );
347static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
348
349/* Done after the current pattern has been matched and before the
350 * corresponding action - sets up yytext.
351 */
352#define YY_DO_BEFORE_ACTION \
353    yyg->yytext_ptr = yy_bp; \
354    yyleng = (size_t) (yy_cp - yy_bp); \
355    yyg->yy_hold_char = *yy_cp; \
356    *yy_cp = '\0'; \
357    yyg->yy_c_buf_p = yy_cp;
358
359#define YY_NUM_RULES 10
360#define YY_END_OF_BUFFER 11
361/* This struct is not used in this scanner,
362   but its presence is necessary. */
363struct yy_trans_info
364    {
365    flex_int32_t yy_verify;
366    flex_int32_t yy_nxt;
367    };
368static yyconst flex_int16_t yy_accept[19] =
369    { 0,
370        0, 0, 0, 0, 11, 5, 3, 1, 4, 2,
371        6, 9, 8, 5, 3, 6, 7, 0
372    } ;
373
374static yyconst flex_int32_t yy_ec[256] =
375    { 0,
376        1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
377        2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
378        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
379        1, 2, 1, 3, 4, 1, 1, 1, 1, 1,
380        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
381        1, 1, 1, 1, 1, 1, 1, 1, 5, 1,
382        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
383        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
384        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
385        1, 6, 1, 1, 1, 1, 1, 1, 1, 1,
386
387        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
388        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
389        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
390        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
391        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
392        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
393        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
394        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
395        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
396        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
397
398        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
399        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
400        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
401        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
402        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
403        1, 1, 1, 1, 1
404    } ;
405
406static yyconst flex_int32_t yy_meta[7] =
407    { 0,
408        1, 2, 3, 4, 4, 5
409    } ;
410
411static yyconst flex_int16_t yy_base[24] =
412    { 0,
413        0, 0, 4, 8, 12, 0, 0, 29, 29, 29,
414        0, 29, 0, 0, 0, 0, 29, 29, 14, 19,
415        7, 24, 5
416    } ;
417
418static yyconst flex_int16_t yy_def[24] =
419    { 0,
420       18, 1, 19, 19, 18, 20, 21, 18, 18, 18,
421       22, 18, 23, 20, 21, 22, 18, 0, 18, 18,
422       18, 18, 18
423    } ;
424
425static yyconst flex_int16_t yy_nxt[36] =
426    { 0,
427        6, 7, 8, 9, 10, 6, 12, 17, 15, 13,
428       12, 18, 18, 13, 11, 11, 11, 11, 11, 14,
429       18, 18, 18, 14, 16, 16, 18, 16, 5, 18,
430       18, 18, 18, 18, 18
431    } ;
432
433static yyconst flex_int16_t yy_chk[36] =
434    { 0,
435        1, 1, 1, 1, 1, 1, 3, 23, 21, 3,
436        4, 5, 0, 4, 19, 19, 19, 19, 19, 20,
437        0, 0, 0, 20, 22, 22, 0, 22, 18, 18,
438       18, 18, 18, 18, 18
439    } ;
440
441/* The intent behind this definition is that it'll catch
442 * any uses of REJECT which flex missed.
443 */
444#define REJECT reject_used_but_not_detected
445#define yymore() yymore_used_but_not_detected
446#define YY_MORE_ADJ 0
447#define YY_RESTORE_YY_MORE_OFFSET
448#line 1 "shell_lex.l"
449/*
450 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
451 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
452 *
453 * This program is free software: you can redistribute it and/or modify
454 * it under the terms of the GNU General Public License as published by
455 * the Free Software Foundation, either version 3 of the License, or
456 * (at your option) any later version.
457 *
458 * This program is distributed in the hope that it will be useful,
459 * but WITHOUT ANY WARRANTY; without even the implied warranty of
460 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
461 * GNU General Public License for more details.
462 *
463 * You should have received a copy of the GNU General Public License
464 * along with this program. If not, see <http://www.gnu.org/licenses/>.
465 */
466#line 20 "shell_lex.l"
467#include "shell_internal.h"
468#include <string.h>
469#include <stdlib.h>
470
471static char *str_append(char *str, const char *src) {
472    if(str) {
473        size_t newlen = strlen(str) + strlen(src) + 1;
474
475        char *newstr = malloc(newlen);
476
477        strcpy(newstr, str);
478        strcat(newstr, src);
479
480        free(str);
481
482        return newstr;
483    } else
484        return strdup(src);
485}
486#define YY_NO_INPUT 1
487
488#line 489 "shell_lex.c"
489
490#define INITIAL 0
491#define STR 1
492
493#ifndef YY_NO_UNISTD_H
494/* Special case for "unistd.h", since it is non-ANSI. We include it way
495 * down here because we want the user's section 1 to have been scanned first.
496 * The user has a chance to override it with an option.
497 */
498#include <unistd.h>
499#endif
500
501#ifndef YY_EXTRA_TYPE
502#define YY_EXTRA_TYPE void *
503#endif
504
505/* Holds the entire state of the reentrant scanner. */
506struct yyguts_t
507    {
508
509    /* User-defined. Not touched by flex. */
510    YY_EXTRA_TYPE yyextra_r;
511
512    /* The rest are the same as the globals declared in the non-reentrant scanner. */
513    FILE *yyin_r, *yyout_r;
514    size_t yy_buffer_stack_top; /**< index of top of stack. */
515    size_t yy_buffer_stack_max; /**< capacity of stack. */
516    YY_BUFFER_STATE * yy_buffer_stack; /**< Stack as an array. */
517    char yy_hold_char;
518    int yy_n_chars;
519    int yyleng_r;
520    char *yy_c_buf_p;
521    int yy_init;
522    int yy_start;
523    int yy_did_buffer_switch_on_eof;
524    int yy_start_stack_ptr;
525    int yy_start_stack_depth;
526    int *yy_start_stack;
527    yy_state_type yy_last_accepting_state;
528    char* yy_last_accepting_cpos;
529
530    int yylineno_r;
531    int yy_flex_debug_r;
532
533    char *yytext_r;
534    int yy_more_flag;
535    int yy_more_len;
536
537    }; /* end struct yyguts_t */
538
539static int yy_init_globals (yyscan_t yyscanner );
540
541int yylex_init (yyscan_t* scanner);
542
543int yylex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner);
544
545/* Accessor methods to globals.
546   These are made visible to non-reentrant scanners for convenience. */
547
548int yylex_destroy (yyscan_t yyscanner );
549
550int yyget_debug (yyscan_t yyscanner );
551
552void yyset_debug (int debug_flag ,yyscan_t yyscanner );
553
554YY_EXTRA_TYPE yyget_extra (yyscan_t yyscanner );
555
556void yyset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
557
558FILE *yyget_in (yyscan_t yyscanner );
559
560void yyset_in (FILE * in_str ,yyscan_t yyscanner );
561
562FILE *yyget_out (yyscan_t yyscanner );
563
564void yyset_out (FILE * out_str ,yyscan_t yyscanner );
565
566int yyget_leng (yyscan_t yyscanner );
567
568char *yyget_text (yyscan_t yyscanner );
569
570int yyget_lineno (yyscan_t yyscanner );
571
572void yyset_lineno (int line_number ,yyscan_t yyscanner );
573
574/* Macros after this point can all be overridden by user definitions in
575 * section 1.
576 */
577
578#ifndef YY_SKIP_YYWRAP
579#ifdef __cplusplus
580extern "C" int yywrap (yyscan_t yyscanner );
581#else
582extern int yywrap (yyscan_t yyscanner );
583#endif
584#endif
585
586#ifndef yytext_ptr
587static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner);
588#endif
589
590#ifdef YY_NEED_STRLEN
591static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner);
592#endif
593
594#ifndef YY_NO_INPUT
595
596#ifdef __cplusplus
597static int yyinput (yyscan_t yyscanner );
598#else
599static int input (yyscan_t yyscanner );
600#endif
601
602#endif
603
604/* Amount of stuff to slurp up with each read. */
605#ifndef YY_READ_BUF_SIZE
606#ifdef __ia64__
607/* On IA-64, the buffer size is 16k, not 8k */
608#define YY_READ_BUF_SIZE 16384
609#else
610#define YY_READ_BUF_SIZE 8192
611#endif /* __ia64__ */
612#endif
613
614/* Copy whatever the last rule matched to the standard output. */
615#ifndef ECHO
616/* This used to be an fputs(), but since the string might contain NUL's,
617 * we now use fwrite().
618 */
619#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)
620#endif
621
622/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
623 * is returned in "result".
624 */
625#ifndef YY_INPUT
626#define YY_INPUT(buf,result,max_size) \
627    if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
628        { \
629        int c = '*'; \
630        size_t n; \
631        for ( n = 0; n < max_size && \
632                 (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
633            buf[n] = (char) c; \
634        if ( c == '\n' ) \
635            buf[n++] = (char) c; \
636        if ( c == EOF && ferror( yyin ) ) \
637            YY_FATAL_ERROR( "input in flex scanner failed" ); \
638        result = n; \
639        } \
640    else \
641        { \
642        errno=0; \
643        while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
644            { \
645            if( errno != EINTR) \
646                { \
647                YY_FATAL_ERROR( "input in flex scanner failed" ); \
648                break; \
649                } \
650            errno=0; \
651            clearerr(yyin); \
652            } \
653        }\
654\
655
656#endif
657
658/* No semi-colon after return; correct usage is to write "yyterminate();" -
659 * we don't want an extra ';' after the "return" because that will cause
660 * some compilers to complain about unreachable statements.
661 */
662#ifndef yyterminate
663#define yyterminate() return YY_NULL
664#endif
665
666/* Number of entries by which start-condition stack grows. */
667#ifndef YY_START_STACK_INCR
668#define YY_START_STACK_INCR 25
669#endif
670
671/* Report a fatal error. */
672#ifndef YY_FATAL_ERROR
673#define YY_FATAL_ERROR(msg) yy_fatal_error( msg , yyscanner)
674#endif
675
676/* end tables serialization structures and prototypes */
677
678/* Default declaration of generated scanner - a define so the user can
679 * easily add parameters.
680 */
681#ifndef YY_DECL
682#define YY_DECL_IS_OURS 1
683
684extern int yylex (yyscan_t yyscanner);
685
686#define YY_DECL int yylex (yyscan_t yyscanner)
687#endif /* !YY_DECL */
688
689/* Code executed at the beginning of each rule, after yytext and yyleng
690 * have been set up.
691 */
692#ifndef YY_USER_ACTION
693#define YY_USER_ACTION
694#endif
695
696/* Code executed at the end of each rule. */
697#ifndef YY_BREAK
698#define YY_BREAK break;
699#endif
700
701#define YY_RULE_SETUP \
702    YY_USER_ACTION
703
704/** The main scanner function which does all the work.
705 */
706YY_DECL
707{
708    register yy_state_type yy_current_state;
709    register char *yy_cp, *yy_bp;
710    register int yy_act;
711    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
712
713#line 43 "shell_lex.l"
714
715#line 716 "shell_lex.c"
716
717    if ( !yyg->yy_init )
718        {
719        yyg->yy_init = 1;
720
721#ifdef YY_USER_INIT
722        YY_USER_INIT;
723#endif
724
725        if ( ! yyg->yy_start )
726            yyg->yy_start = 1; /* first start state */
727
728        if ( ! yyin )
729            yyin = stdin;
730
731        if ( ! yyout )
732            yyout = stdout;
733
734        if ( ! YY_CURRENT_BUFFER ) {
735            yyensure_buffer_stack (yyscanner);
736            YY_CURRENT_BUFFER_LVALUE =
737                yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner);
738        }
739
740        yy_load_buffer_state(yyscanner );
741        }
742
743    while ( 1 ) /* loops until end-of-file is reached */
744        {
745        yy_cp = yyg->yy_c_buf_p;
746
747        /* Support of yytext. */
748        *yy_cp = yyg->yy_hold_char;
749
750        /* yy_bp points to the position in yy_ch_buf of the start of
751         * the current run.
752         */
753        yy_bp = yy_cp;
754
755        yy_current_state = yyg->yy_start;
756yy_match:
757        do
758            {
759            register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
760            if ( yy_accept[yy_current_state] )
761                {
762                yyg->yy_last_accepting_state = yy_current_state;
763                yyg->yy_last_accepting_cpos = yy_cp;
764                }
765            while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
766                {
767                yy_current_state = (int) yy_def[yy_current_state];
768                if ( yy_current_state >= 19 )
769                    yy_c = yy_meta[(unsigned int) yy_c];
770                }
771            yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
772            ++yy_cp;
773            }
774        while ( yy_current_state != 18 );
775        yy_cp = yyg->yy_last_accepting_cpos;
776        yy_current_state = yyg->yy_last_accepting_state;
777
778yy_find_action:
779        yy_act = yy_accept[yy_current_state];
780
781        YY_DO_BEFORE_ACTION;
782
783do_action: /* This label is used only to access EOF actions. */
784
785        switch ( yy_act )
786    { /* beginning of action switch */
787            case 0: /* must back up */
788            /* undo the effects of YY_DO_BEFORE_ACTION */
789            *yy_cp = yyg->yy_hold_char;
790            yy_cp = yyg->yy_last_accepting_cpos;
791            yy_current_state = yyg->yy_last_accepting_state;
792            goto yy_find_action;
793
794case 1:
795YY_RULE_SETUP
796#line 44 "shell_lex.l"
797{ yyextra->strval = NULL; BEGIN(STR); }
798    YY_BREAK
799case 2:
800YY_RULE_SETUP
801#line 45 "shell_lex.l"
802return TOK_SEPARATOR;
803    YY_BREAK
804case 3:
805/* rule 3 can match eol */
806YY_RULE_SETUP
807#line 46 "shell_lex.l"
808return TOK_SPACE;
809    YY_BREAK
810case 4:
811YY_RULE_SETUP
812#line 47 "shell_lex.l"
813return TOK_COMMENT;
814    YY_BREAK
815case 5:
816YY_RULE_SETUP
817#line 48 "shell_lex.l"
818{ yyextra->strval = strdup(yytext); return TOK_STRING; }
819    YY_BREAK
820case 6:
821/* rule 6 can match eol */
822YY_RULE_SETUP
823#line 49 "shell_lex.l"
824yyextra->strval = str_append(yyextra->strval, yytext);
825    YY_BREAK
826case 7:
827YY_RULE_SETUP
828#line 50 "shell_lex.l"
829yyextra->strval = str_append(yyextra->strval, "\"");
830    YY_BREAK
831case 8:
832YY_RULE_SETUP
833#line 51 "shell_lex.l"
834yyextra->strval = str_append(yyextra->strval, "\\");
835    YY_BREAK
836case 9:
837YY_RULE_SETUP
838#line 52 "shell_lex.l"
839{ BEGIN(INITIAL); return TOK_STRING; }
840    YY_BREAK
841case 10:
842YY_RULE_SETUP
843#line 53 "shell_lex.l"
844ECHO;
845    YY_BREAK
846#line 847 "shell_lex.c"
847case YY_STATE_EOF(INITIAL):
848case YY_STATE_EOF(STR):
849    yyterminate();
850
851    case YY_END_OF_BUFFER:
852        {
853        /* Amount of text matched not including the EOB char. */
854        int yy_amount_of_matched_text = (int) (yy_cp - yyg->yytext_ptr) - 1;
855
856        /* Undo the effects of YY_DO_BEFORE_ACTION. */
857        *yy_cp = yyg->yy_hold_char;
858        YY_RESTORE_YY_MORE_OFFSET
859
860        if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW )
861            {
862            /* We're scanning a new file or input source. It's
863             * possible that this happened because the user
864             * just pointed yyin at a new source and called
865             * yylex(). If so, then we have to assure
866             * consistency between YY_CURRENT_BUFFER and our
867             * globals. Here is the right place to do so, because
868             * this is the first action (other than possibly a
869             * back-up) that will match for the new input source.
870             */
871            yyg->yy_n_chars = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
872            YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin;
873            YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL;
874            }
875
876        /* Note that here we test for yy_c_buf_p "<=" to the position
877         * of the first EOB in the buffer, since yy_c_buf_p will
878         * already have been incremented past the NUL character
879         * (since all states make transitions on EOB to the
880         * end-of-buffer state). Contrast this with the test
881         * in input().
882         */
883        if ( yyg->yy_c_buf_p <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] )
884            { /* This was really a NUL. */
885            yy_state_type yy_next_state;
886
887            yyg->yy_c_buf_p = yyg->yytext_ptr + yy_amount_of_matched_text;
888
889            yy_current_state = yy_get_previous_state( yyscanner );
890
891            /* Okay, we're now positioned to make the NUL
892             * transition. We couldn't have
893             * yy_get_previous_state() go ahead and do it
894             * for us because it doesn't know how to deal
895             * with the possibility of jamming (and we don't
896             * want to build jamming into it because then it
897             * will run more slowly).
898             */
899
900            yy_next_state = yy_try_NUL_trans( yy_current_state , yyscanner);
901
902            yy_bp = yyg->yytext_ptr + YY_MORE_ADJ;
903
904            if ( yy_next_state )
905                {
906                /* Consume the NUL. */
907                yy_cp = ++yyg->yy_c_buf_p;
908                yy_current_state = yy_next_state;
909                goto yy_match;
910                }
911
912            else
913                {
914                yy_cp = yyg->yy_last_accepting_cpos;
915                yy_current_state = yyg->yy_last_accepting_state;
916                goto yy_find_action;
917                }
918            }
919
920        else switch ( yy_get_next_buffer( yyscanner ) )
921            {
922            case EOB_ACT_END_OF_FILE:
923                {
924                yyg->yy_did_buffer_switch_on_eof = 0;
925
926                if ( yywrap(yyscanner ) )
927                    {
928                    /* Note: because we've taken care in
929                     * yy_get_next_buffer() to have set up
930                     * yytext, we can now set up
931                     * yy_c_buf_p so that if some total
932                     * hoser (like flex itself) wants to
933                     * call the scanner after we return the
934                     * YY_NULL, it'll still work - another
935                     * YY_NULL will get returned.
936                     */
937                    yyg->yy_c_buf_p = yyg->yytext_ptr + YY_MORE_ADJ;
938
939                    yy_act = YY_STATE_EOF(YY_START);
940                    goto do_action;
941                    }
942
943                else
944                    {
945                    if ( ! yyg->yy_did_buffer_switch_on_eof )
946                        YY_NEW_FILE;
947                    }
948                break;
949                }
950
951            case EOB_ACT_CONTINUE_SCAN:
952                yyg->yy_c_buf_p =
953                    yyg->yytext_ptr + yy_amount_of_matched_text;
954
955                yy_current_state = yy_get_previous_state( yyscanner );
956
957                yy_cp = yyg->yy_c_buf_p;
958                yy_bp = yyg->yytext_ptr + YY_MORE_ADJ;
959                goto yy_match;
960
961            case EOB_ACT_LAST_MATCH:
962                yyg->yy_c_buf_p =
963                &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars];
964
965                yy_current_state = yy_get_previous_state( yyscanner );
966
967                yy_cp = yyg->yy_c_buf_p;
968                yy_bp = yyg->yytext_ptr + YY_MORE_ADJ;
969                goto yy_find_action;
970            }
971        break;
972        }
973
974    default:
975        YY_FATAL_ERROR(
976            "fatal flex scanner internal error--no action found" );
977    } /* end of action switch */
978        } /* end of scanning one token */
979} /* end of yylex */
980
981/* yy_get_next_buffer - try to read in a new buffer
982 *
983 * Returns a code representing an action:
984 * EOB_ACT_LAST_MATCH -
985 * EOB_ACT_CONTINUE_SCAN - continue scanning from current position
986 * EOB_ACT_END_OF_FILE - end of file
987 */
988static int yy_get_next_buffer (yyscan_t yyscanner)
989{
990    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
991    register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
992    register char *source = yyg->yytext_ptr;
993    register int number_to_move, i;
994    int ret_val;
995
996    if ( yyg->yy_c_buf_p > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] )
997        YY_FATAL_ERROR(
998        "fatal flex scanner internal error--end of buffer missed" );
999
1000    if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 )
1001        { /* Don't try to fill the buffer, so this is an EOF. */
1002        if ( yyg->yy_c_buf_p - yyg->yytext_ptr - YY_MORE_ADJ == 1 )
1003            {
1004            /* We matched a single character, the EOB, so
1005             * treat this as a final EOF.
1006             */
1007            return EOB_ACT_END_OF_FILE;
1008            }
1009
1010        else
1011            {
1012            /* We matched some text prior to the EOB, first
1013             * process it.
1014             */
1015            return EOB_ACT_LAST_MATCH;
1016            }
1017        }
1018
1019    /* Try to read more data. */
1020
1021    /* First move last chars to start of buffer. */
1022    number_to_move = (int) (yyg->yy_c_buf_p - yyg->yytext_ptr) - 1;
1023
1024    for ( i = 0; i < number_to_move; ++i )
1025        *(dest++) = *(source++);
1026
1027    if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING )
1028        /* don't do the read, it's not guaranteed to return an EOF,
1029         * just force an EOF
1030         */
1031        YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars = 0;
1032
1033    else
1034        {
1035            int num_to_read =
1036            YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
1037
1038        while ( num_to_read <= 0 )
1039            { /* Not enough room in the buffer - grow it. */
1040
1041            /* just a shorter name for the current buffer */
1042            YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
1043
1044            int yy_c_buf_p_offset =
1045                (int) (yyg->yy_c_buf_p - b->yy_ch_buf);
1046
1047            if ( b->yy_is_our_buffer )
1048                {
1049                int new_size = b->yy_buf_size * 2;
1050
1051                if ( new_size <= 0 )
1052                    b->yy_buf_size += b->yy_buf_size / 8;
1053                else
1054                    b->yy_buf_size *= 2;
1055
1056                b->yy_ch_buf = (char *)
1057                    /* Include room in for 2 EOB chars. */
1058                    yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ,yyscanner );
1059                }
1060            else
1061                /* Can't grow it, we don't own it. */
1062                b->yy_ch_buf = 0;
1063
1064            if ( ! b->yy_ch_buf )
1065                YY_FATAL_ERROR(
1066                "fatal error - scanner input buffer overflow" );
1067
1068            yyg->yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset];
1069
1070            num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size -
1071                        number_to_move - 1;
1072
1073            }
1074
1075        if ( num_to_read > YY_READ_BUF_SIZE )
1076            num_to_read = YY_READ_BUF_SIZE;
1077
1078        /* Read in more data. */
1079        YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
1080            yyg->yy_n_chars, (size_t) num_to_read );
1081
1082        YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars;
1083        }
1084
1085    if ( yyg->yy_n_chars == 0 )
1086        {
1087        if ( number_to_move == YY_MORE_ADJ )
1088            {
1089            ret_val = EOB_ACT_END_OF_FILE;
1090            yyrestart(yyin ,yyscanner);
1091            }
1092
1093        else
1094            {
1095            ret_val = EOB_ACT_LAST_MATCH;
1096            YY_CURRENT_BUFFER_LVALUE->yy_buffer_status =
1097                YY_BUFFER_EOF_PENDING;
1098            }
1099        }
1100
1101    else
1102        ret_val = EOB_ACT_CONTINUE_SCAN;
1103
1104    if ((yy_size_t) (yyg->yy_n_chars + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
1105        /* Extend the array by 50%, plus the number we really need. */
1106        yy_size_t new_size = yyg->yy_n_chars + number_to_move + (yyg->yy_n_chars >> 1);
1107        YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ,yyscanner );
1108        if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
1109            YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
1110    }
1111
1112    yyg->yy_n_chars += number_to_move;
1113    YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] = YY_END_OF_BUFFER_CHAR;
1114    YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR;
1115
1116    yyg->yytext_ptr = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0];
1117
1118    return ret_val;
1119}
1120
1121/* yy_get_previous_state - get the state just before the EOB char was reached */
1122
1123    static yy_state_type yy_get_previous_state (yyscan_t yyscanner)
1124{
1125    register yy_state_type yy_current_state;
1126    register char *yy_cp;
1127    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1128
1129    yy_current_state = yyg->yy_start;
1130
1131    for ( yy_cp = yyg->yytext_ptr + YY_MORE_ADJ; yy_cp < yyg->yy_c_buf_p; ++yy_cp )
1132        {
1133        register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
1134        if ( yy_accept[yy_current_state] )
1135            {
1136            yyg->yy_last_accepting_state = yy_current_state;
1137            yyg->yy_last_accepting_cpos = yy_cp;
1138            }
1139        while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
1140            {
1141            yy_current_state = (int) yy_def[yy_current_state];
1142            if ( yy_current_state >= 19 )
1143                yy_c = yy_meta[(unsigned int) yy_c];
1144            }
1145        yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
1146        }
1147
1148    return yy_current_state;
1149}
1150
1151/* yy_try_NUL_trans - try to make a transition on the NUL character
1152 *
1153 * synopsis
1154 * next_state = yy_try_NUL_trans( current_state );
1155 */
1156    static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state , yyscan_t yyscanner)
1157{
1158    register int yy_is_jam;
1159    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; /* This var may be unused depending upon options. */
1160    register char *yy_cp = yyg->yy_c_buf_p;
1161
1162    register YY_CHAR yy_c = 1;
1163    if ( yy_accept[yy_current_state] )
1164        {
1165        yyg->yy_last_accepting_state = yy_current_state;
1166        yyg->yy_last_accepting_cpos = yy_cp;
1167        }
1168    while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
1169        {
1170        yy_current_state = (int) yy_def[yy_current_state];
1171        if ( yy_current_state >= 19 )
1172            yy_c = yy_meta[(unsigned int) yy_c];
1173        }
1174    yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
1175    yy_is_jam = (yy_current_state == 18);
1176
1177    return yy_is_jam ? 0 : yy_current_state;
1178}
1179
1180#ifndef YY_NO_INPUT
1181#ifdef __cplusplus
1182    static int yyinput (yyscan_t yyscanner)
1183#else
1184    static int input (yyscan_t yyscanner)
1185#endif
1186
1187{
1188    int c;
1189    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1190
1191    *yyg->yy_c_buf_p = yyg->yy_hold_char;
1192
1193    if ( *yyg->yy_c_buf_p == YY_END_OF_BUFFER_CHAR )
1194        {
1195        /* yy_c_buf_p now points to the character we want to return.
1196         * If this occurs *before* the EOB characters, then it's a
1197         * valid NUL; if not, then we've hit the end of the buffer.
1198         */
1199        if ( yyg->yy_c_buf_p < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] )
1200            /* This was really a NUL. */
1201            *yyg->yy_c_buf_p = '\0';
1202
1203        else
1204            { /* need more input */
1205            int offset = yyg->yy_c_buf_p - yyg->yytext_ptr;
1206            ++yyg->yy_c_buf_p;
1207
1208            switch ( yy_get_next_buffer( yyscanner ) )
1209                {
1210                case EOB_ACT_LAST_MATCH:
1211                    /* This happens because yy_g_n_b()
1212                     * sees that we've accumulated a
1213                     * token and flags that we need to
1214                     * try matching the token before
1215                     * proceeding. But for input(),
1216                     * there's no matching to consider.
1217                     * So convert the EOB_ACT_LAST_MATCH
1218                     * to EOB_ACT_END_OF_FILE.
1219                     */
1220
1221                    /* Reset buffer status. */
1222                    yyrestart(yyin ,yyscanner);
1223
1224                    /*FALLTHROUGH*/
1225
1226                case EOB_ACT_END_OF_FILE:
1227                    {
1228                    if ( yywrap(yyscanner ) )
1229                        return EOF;
1230
1231                    if ( ! yyg->yy_did_buffer_switch_on_eof )
1232                        YY_NEW_FILE;
1233#ifdef __cplusplus
1234                    return yyinput(yyscanner);
1235#else
1236                    return input(yyscanner);
1237#endif
1238                    }
1239
1240                case EOB_ACT_CONTINUE_SCAN:
1241                    yyg->yy_c_buf_p = yyg->yytext_ptr + offset;
1242                    break;
1243                }
1244            }
1245        }
1246
1247    c = *(unsigned char *) yyg->yy_c_buf_p; /* cast for 8-bit char's */
1248    *yyg->yy_c_buf_p = '\0'; /* preserve yytext */
1249    yyg->yy_hold_char = *++yyg->yy_c_buf_p;
1250
1251    return c;
1252}
1253#endif /* ifndef YY_NO_INPUT */
1254
1255/** Immediately switch to a different input stream.
1256 * @param input_file A readable stream.
1257 * @param yyscanner The scanner object.
1258 * @note This function does not reset the start condition to @c INITIAL .
1259 */
1260    void yyrestart (FILE * input_file , yyscan_t yyscanner)
1261{
1262    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1263
1264    if ( ! YY_CURRENT_BUFFER ){
1265        yyensure_buffer_stack (yyscanner);
1266        YY_CURRENT_BUFFER_LVALUE =
1267            yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner);
1268    }
1269
1270    yy_init_buffer(YY_CURRENT_BUFFER,input_file ,yyscanner);
1271    yy_load_buffer_state(yyscanner );
1272}
1273
1274/** Switch to a different input buffer.
1275 * @param new_buffer The new input buffer.
1276 * @param yyscanner The scanner object.
1277 */
1278    void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer , yyscan_t yyscanner)
1279{
1280    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1281
1282    /* TODO. We should be able to replace this entire function body
1283     * with
1284     * yypop_buffer_state();
1285     * yypush_buffer_state(new_buffer);
1286     */
1287    yyensure_buffer_stack (yyscanner);
1288    if ( YY_CURRENT_BUFFER == new_buffer )
1289        return;
1290
1291    if ( YY_CURRENT_BUFFER )
1292        {
1293        /* Flush out information for old buffer. */
1294        *yyg->yy_c_buf_p = yyg->yy_hold_char;
1295        YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = yyg->yy_c_buf_p;
1296        YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars;
1297        }
1298
1299    YY_CURRENT_BUFFER_LVALUE = new_buffer;
1300    yy_load_buffer_state(yyscanner );
1301
1302    /* We don't actually know whether we did this switch during
1303     * EOF (yywrap()) processing, but the only time this flag
1304     * is looked at is after yywrap() is called, so it's safe
1305     * to go ahead and always set it.
1306     */
1307    yyg->yy_did_buffer_switch_on_eof = 1;
1308}
1309
1310static void yy_load_buffer_state (yyscan_t yyscanner)
1311{
1312    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1313    yyg->yy_n_chars = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
1314    yyg->yytext_ptr = yyg->yy_c_buf_p = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos;
1315    yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file;
1316    yyg->yy_hold_char = *yyg->yy_c_buf_p;
1317}
1318
1319/** Allocate and initialize an input buffer state.
1320 * @param file A readable stream.
1321 * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE.
1322 * @param yyscanner The scanner object.
1323 * @return the allocated buffer state.
1324 */
1325    YY_BUFFER_STATE yy_create_buffer (FILE * file, int size , yyscan_t yyscanner)
1326{
1327    YY_BUFFER_STATE b;
1328
1329    b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ,yyscanner );
1330    if ( ! b )
1331        YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1332
1333    b->yy_buf_size = size;
1334
1335    /* yy_ch_buf has to be 2 characters longer than the size given because
1336     * we need to put in 2 end-of-buffer characters.
1337     */
1338    b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ,yyscanner );
1339    if ( ! b->yy_ch_buf )
1340        YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
1341
1342    b->yy_is_our_buffer = 1;
1343
1344    yy_init_buffer(b,file ,yyscanner);
1345
1346    return b;
1347}
1348
1349/** Destroy the buffer.
1350 * @param b a buffer created with yy_create_buffer()
1351 * @param yyscanner The scanner object.
1352 */
1353    void yy_delete_buffer (YY_BUFFER_STATE b , yyscan_t yyscanner)
1354{
1355    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1356
1357    if ( ! b )
1358        return;
1359
1360    if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */
1361        YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0;
1362
1363    if ( b->yy_is_our_buffer )
1364        yyfree((void *) b->yy_ch_buf ,yyscanner );
1365
1366    yyfree((void *) b ,yyscanner );
1367}
1368
1369#ifndef __cplusplus
1370extern int isatty (int );
1371#endif /* __cplusplus */
1372
1373/* Initializes or reinitializes a buffer.
1374 * This function is sometimes called more than once on the same buffer,
1375 * such as during a yyrestart() or at EOF.
1376 */
1377    static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file , yyscan_t yyscanner)
1378
1379{
1380    int oerrno = errno;
1381    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1382
1383    yy_flush_buffer(b ,yyscanner);
1384
1385    b->yy_input_file = file;
1386    b->yy_fill_buffer = 1;
1387
1388    /* If b is the current buffer, then yy_init_buffer was _probably_
1389     * called from yyrestart() or through yy_get_next_buffer.
1390     * In that case, we don't want to reset the lineno or column.
1391     */
1392    if (b != YY_CURRENT_BUFFER){
1393        b->yy_bs_lineno = 1;
1394        b->yy_bs_column = 0;
1395    }
1396
1397        b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
1398
1399    errno = oerrno;
1400}
1401
1402/** Discard all buffered characters. On the next scan, YY_INPUT will be called.
1403 * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER.
1404 * @param yyscanner The scanner object.
1405 */
1406    void yy_flush_buffer (YY_BUFFER_STATE b , yyscan_t yyscanner)
1407{
1408    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1409    if ( ! b )
1410        return;
1411
1412    b->yy_n_chars = 0;
1413
1414    /* We always need two end-of-buffer characters. The first causes
1415     * a transition to the end-of-buffer state. The second causes
1416     * a jam in that state.
1417     */
1418    b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR;
1419    b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR;
1420
1421    b->yy_buf_pos = &b->yy_ch_buf[0];
1422
1423    b->yy_at_bol = 1;
1424    b->yy_buffer_status = YY_BUFFER_NEW;
1425
1426    if ( b == YY_CURRENT_BUFFER )
1427        yy_load_buffer_state(yyscanner );
1428}
1429
1430/** Pushes the new state onto the stack. The new state becomes
1431 * the current state. This function will allocate the stack
1432 * if necessary.
1433 * @param new_buffer The new state.
1434 * @param yyscanner The scanner object.
1435 */
1436void yypush_buffer_state (YY_BUFFER_STATE new_buffer , yyscan_t yyscanner)
1437{
1438    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1439    if (new_buffer == NULL)
1440        return;
1441
1442    yyensure_buffer_stack(yyscanner);
1443
1444    /* This block is copied from yy_switch_to_buffer. */
1445    if ( YY_CURRENT_BUFFER )
1446        {
1447        /* Flush out information for old buffer. */
1448        *yyg->yy_c_buf_p = yyg->yy_hold_char;
1449        YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = yyg->yy_c_buf_p;
1450        YY_CURRENT_BUFFER_LVALUE->yy_n_chars = yyg->yy_n_chars;
1451        }
1452
1453    /* Only push if top exists. Otherwise, replace top. */
1454    if (YY_CURRENT_BUFFER)
1455        yyg->yy_buffer_stack_top++;
1456    YY_CURRENT_BUFFER_LVALUE = new_buffer;
1457
1458    /* copied from yy_switch_to_buffer. */
1459    yy_load_buffer_state(yyscanner );
1460    yyg->yy_did_buffer_switch_on_eof = 1;
1461}
1462
1463/** Removes and deletes the top of the stack, if present.
1464 * The next element becomes the new top.
1465 * @param yyscanner The scanner object.
1466 */
1467void yypop_buffer_state (yyscan_t yyscanner)
1468{
1469    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1470    if (!YY_CURRENT_BUFFER)
1471        return;
1472
1473    yy_delete_buffer(YY_CURRENT_BUFFER ,yyscanner);
1474    YY_CURRENT_BUFFER_LVALUE = NULL;
1475    if (yyg->yy_buffer_stack_top > 0)
1476        --yyg->yy_buffer_stack_top;
1477
1478    if (YY_CURRENT_BUFFER) {
1479        yy_load_buffer_state(yyscanner );
1480        yyg->yy_did_buffer_switch_on_eof = 1;
1481    }
1482}
1483
1484/* Allocates the stack if it does not exist.
1485 * Guarantees space for at least one push.
1486 */
1487static void yyensure_buffer_stack (yyscan_t yyscanner)
1488{
1489    int num_to_alloc;
1490    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1491
1492    if (!yyg->yy_buffer_stack) {
1493
1494        /* First allocation is just for 2 elements, since we don't know if this
1495         * scanner will even need a stack. We use 2 instead of 1 to avoid an
1496         * immediate realloc on the next call.
1497         */
1498        num_to_alloc = 1;
1499        yyg->yy_buffer_stack = (struct yy_buffer_state**)yyalloc
1500                                (num_to_alloc * sizeof(struct yy_buffer_state*)
1501                                , yyscanner);
1502        if ( ! yyg->yy_buffer_stack )
1503            YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
1504
1505        memset(yyg->yy_buffer_stack, 0, num_to_alloc * sizeof(struct yy_buffer_state*));
1506
1507        yyg->yy_buffer_stack_max = num_to_alloc;
1508        yyg->yy_buffer_stack_top = 0;
1509        return;
1510    }
1511
1512    if (yyg->yy_buffer_stack_top >= (yyg->yy_buffer_stack_max) - 1){
1513
1514        /* Increase the buffer to prepare for a possible push. */
1515        int grow_size = 8 /* arbitrary grow size */;
1516
1517        num_to_alloc = yyg->yy_buffer_stack_max + grow_size;
1518        yyg->yy_buffer_stack = (struct yy_buffer_state**)yyrealloc
1519                                (yyg->yy_buffer_stack,
1520                                num_to_alloc * sizeof(struct yy_buffer_state*)
1521                                , yyscanner);
1522        if ( ! yyg->yy_buffer_stack )
1523            YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
1524
1525        /* zero only the new slots.*/
1526        memset(yyg->yy_buffer_stack + yyg->yy_buffer_stack_max, 0, grow_size * sizeof(struct yy_buffer_state*));
1527        yyg->yy_buffer_stack_max = num_to_alloc;
1528    }
1529}
1530
1531/** Setup the input buffer state to scan directly from a user-specified character buffer.
1532 * @param base the character buffer
1533 * @param size the size in bytes of the character buffer
1534 * @param yyscanner The scanner object.
1535 * @return the newly allocated buffer state object.
1536 */
1537YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size , yyscan_t yyscanner)
1538{
1539    YY_BUFFER_STATE b;
1540
1541    if ( size < 2 ||
1542         base[size-2] != YY_END_OF_BUFFER_CHAR ||
1543         base[size-1] != YY_END_OF_BUFFER_CHAR )
1544        /* They forgot to leave room for the EOB's. */
1545        return 0;
1546
1547    b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ,yyscanner );
1548    if ( ! b )
1549        YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );
1550
1551    b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */
1552    b->yy_buf_pos = b->yy_ch_buf = base;
1553    b->yy_is_our_buffer = 0;
1554    b->yy_input_file = 0;
1555    b->yy_n_chars = b->yy_buf_size;
1556    b->yy_is_interactive = 0;
1557    b->yy_at_bol = 1;
1558    b->yy_fill_buffer = 0;
1559    b->yy_buffer_status = YY_BUFFER_NEW;
1560
1561    yy_switch_to_buffer(b ,yyscanner );
1562
1563    return b;
1564}
1565
1566/** Setup the input buffer state to scan a string. The next call to yylex() will
1567 * scan from a @e copy of @a str.
1568 * @param yystr a NUL-terminated string to scan
1569 * @param yyscanner The scanner object.
1570 * @return the newly allocated buffer state object.
1571 * @note If you want to scan bytes that may contain NUL values, then use
1572 * yy_scan_bytes() instead.
1573 */
1574YY_BUFFER_STATE yy_scan_string (yyconst char * yystr , yyscan_t yyscanner)
1575{
1576
1577    return yy_scan_bytes(yystr,strlen(yystr) ,yyscanner);
1578}
1579
1580/** Setup the input buffer state to scan the given bytes. The next call to yylex() will
1581 * scan from a @e copy of @a bytes.
1582 * @param yybytes the byte buffer to scan
1583 * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes.
1584 * @param yyscanner The scanner object.
1585 * @return the newly allocated buffer state object.
1586 */
1587YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, int _yybytes_len , yyscan_t yyscanner)
1588{
1589    YY_BUFFER_STATE b;
1590    char *buf;
1591    yy_size_t n;
1592    int i;
1593
1594    /* Get memory for full buffer, including space for trailing EOB's. */
1595    n = _yybytes_len + 2;
1596    buf = (char *) yyalloc(n ,yyscanner );
1597    if ( ! buf )
1598        YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
1599
1600    for ( i = 0; i < _yybytes_len; ++i )
1601        buf[i] = yybytes[i];
1602
1603    buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR;
1604
1605    b = yy_scan_buffer(buf,n ,yyscanner);
1606    if ( ! b )
1607        YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );
1608
1609    /* It's okay to grow etc. this buffer, and we should throw it
1610     * away when we're done.
1611     */
1612    b->yy_is_our_buffer = 1;
1613
1614    return b;
1615}
1616
1617#ifndef YY_EXIT_FAILURE
1618#define YY_EXIT_FAILURE 2
1619#endif
1620
1621static void yy_fatal_error (yyconst char* msg , yyscan_t yyscanner)
1622{
1623        (void) fprintf( stderr, "%s\n", msg );
1624    exit( YY_EXIT_FAILURE );
1625}
1626
1627/* Redefine yyless() so it works in section 3 code. */
1628
1629#undef yyless
1630#define yyless(n) \
1631    do \
1632        { \
1633        /* Undo effects of setting up yytext. */ \
1634        int yyless_macro_arg = (n); \
1635        YY_LESS_LINENO(yyless_macro_arg);\
1636        yytext[yyleng] = yyg->yy_hold_char; \
1637        yyg->yy_c_buf_p = yytext + yyless_macro_arg; \
1638        yyg->yy_hold_char = *yyg->yy_c_buf_p; \
1639        *yyg->yy_c_buf_p = '\0'; \
1640        yyleng = yyless_macro_arg; \
1641        } \
1642    while ( 0 )
1643
1644/* Accessor methods (get/set functions) to struct members. */
1645
1646/** Get the user-defined data for this scanner.
1647 * @param yyscanner The scanner object.
1648 */
1649YY_EXTRA_TYPE yyget_extra (yyscan_t yyscanner)
1650{
1651    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1652    return yyextra;
1653}
1654
1655/** Get the current line number.
1656 * @param yyscanner The scanner object.
1657 */
1658int yyget_lineno (yyscan_t yyscanner)
1659{
1660    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1661
1662        if (! YY_CURRENT_BUFFER)
1663            return 0;
1664
1665    return yylineno;
1666}
1667
1668/** Get the current column number.
1669 * @param yyscanner The scanner object.
1670 */
1671int yyget_column (yyscan_t yyscanner)
1672{
1673    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1674
1675        if (! YY_CURRENT_BUFFER)
1676            return 0;
1677
1678    return yycolumn;
1679}
1680
1681/** Get the input stream.
1682 * @param yyscanner The scanner object.
1683 */
1684FILE *yyget_in (yyscan_t yyscanner)
1685{
1686    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1687    return yyin;
1688}
1689
1690/** Get the output stream.
1691 * @param yyscanner The scanner object.
1692 */
1693FILE *yyget_out (yyscan_t yyscanner)
1694{
1695    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1696    return yyout;
1697}
1698
1699/** Get the length of the current token.
1700 * @param yyscanner The scanner object.
1701 */
1702int yyget_leng (yyscan_t yyscanner)
1703{
1704    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1705    return yyleng;
1706}
1707
1708/** Get the current token.
1709 * @param yyscanner The scanner object.
1710 */
1711
1712char *yyget_text (yyscan_t yyscanner)
1713{
1714    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1715    return yytext;
1716}
1717
1718/** Set the user-defined data. This data is never touched by the scanner.
1719 * @param user_defined The data to be associated with this scanner.
1720 * @param yyscanner The scanner object.
1721 */
1722void yyset_extra (YY_EXTRA_TYPE user_defined , yyscan_t yyscanner)
1723{
1724    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1725    yyextra = user_defined ;
1726}
1727
1728/** Set the current line number.
1729 * @param line_number
1730 * @param yyscanner The scanner object.
1731 */
1732void yyset_lineno (int line_number , yyscan_t yyscanner)
1733{
1734    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1735
1736        /* lineno is only valid if an input buffer exists. */
1737        if (! YY_CURRENT_BUFFER )
1738           yy_fatal_error( "yyset_lineno called with no buffer" , yyscanner);
1739
1740    yylineno = line_number;
1741}
1742
1743/** Set the current column.
1744 * @param line_number
1745 * @param yyscanner The scanner object.
1746 */
1747void yyset_column (int column_no , yyscan_t yyscanner)
1748{
1749    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1750
1751        /* column is only valid if an input buffer exists. */
1752        if (! YY_CURRENT_BUFFER )
1753           yy_fatal_error( "yyset_column called with no buffer" , yyscanner);
1754
1755    yycolumn = column_no;
1756}
1757
1758/** Set the input stream. This does not discard the current
1759 * input buffer.
1760 * @param in_str A readable stream.
1761 * @param yyscanner The scanner object.
1762 * @see yy_switch_to_buffer
1763 */
1764void yyset_in (FILE * in_str , yyscan_t yyscanner)
1765{
1766    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1767    yyin = in_str ;
1768}
1769
1770void yyset_out (FILE * out_str , yyscan_t yyscanner)
1771{
1772    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1773    yyout = out_str ;
1774}
1775
1776int yyget_debug (yyscan_t yyscanner)
1777{
1778    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1779    return yy_flex_debug;
1780}
1781
1782void yyset_debug (int bdebug , yyscan_t yyscanner)
1783{
1784    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1785    yy_flex_debug = bdebug ;
1786}
1787
1788/* Accessor methods for yylval and yylloc */
1789
1790/* User-visible API */
1791
1792/* yylex_init is special because it creates the scanner itself, so it is
1793 * the ONLY reentrant function that doesn't take the scanner as the last argument.
1794 * That's why we explicitly handle the declaration, instead of using our macros.
1795 */
1796
1797int yylex_init(yyscan_t* ptr_yy_globals)
1798
1799{
1800    if (ptr_yy_globals == NULL){
1801        errno = EINVAL;
1802        return 1;
1803    }
1804
1805    *ptr_yy_globals = (yyscan_t) yyalloc ( sizeof( struct yyguts_t ), NULL );
1806
1807    if (*ptr_yy_globals == NULL){
1808        errno = ENOMEM;
1809        return 1;
1810    }
1811
1812    /* By setting to 0xAA, we expose bugs in yy_init_globals. Leave at 0x00 for releases. */
1813    memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t));
1814
1815    return yy_init_globals ( *ptr_yy_globals );
1816}
1817
1818/* yylex_init_extra has the same functionality as yylex_init, but follows the
1819 * convention of taking the scanner as the last argument. Note however, that
1820 * this is a *pointer* to a scanner, as it will be allocated by this call (and
1821 * is the reason, too, why this function also must handle its own declaration).
1822 * The user defined value in the first argument will be available to yyalloc in
1823 * the yyextra field.
1824 */
1825
1826int yylex_init_extra(YY_EXTRA_TYPE yy_user_defined,yyscan_t* ptr_yy_globals )
1827
1828{
1829    struct yyguts_t dummy_yyguts;
1830
1831    yyset_extra (yy_user_defined, &dummy_yyguts);
1832
1833    if (ptr_yy_globals == NULL){
1834        errno = EINVAL;
1835        return 1;
1836    }
1837
1838    *ptr_yy_globals = (yyscan_t) yyalloc ( sizeof( struct yyguts_t ), &dummy_yyguts );
1839
1840    if (*ptr_yy_globals == NULL){
1841        errno = ENOMEM;
1842        return 1;
1843    }
1844
1845    /* By setting to 0xAA, we expose bugs in
1846    yy_init_globals. Leave at 0x00 for releases. */
1847    memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t));
1848
1849    yyset_extra (yy_user_defined, *ptr_yy_globals);
1850
1851    return yy_init_globals ( *ptr_yy_globals );
1852}
1853
1854static int yy_init_globals (yyscan_t yyscanner)
1855{
1856    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1857    /* Initialization is the same as for the non-reentrant scanner.
1858     * This function is called from yylex_destroy(), so don't allocate here.
1859     */
1860
1861    yyg->yy_buffer_stack = 0;
1862    yyg->yy_buffer_stack_top = 0;
1863    yyg->yy_buffer_stack_max = 0;
1864    yyg->yy_c_buf_p = (char *) 0;
1865    yyg->yy_init = 0;
1866    yyg->yy_start = 0;
1867
1868    yyg->yy_start_stack_ptr = 0;
1869    yyg->yy_start_stack_depth = 0;
1870    yyg->yy_start_stack = NULL;
1871
1872/* Defined in main.c */
1873#ifdef YY_STDINIT
1874    yyin = stdin;
1875    yyout = stdout;
1876#else
1877    yyin = (FILE *) 0;
1878    yyout = (FILE *) 0;
1879#endif
1880
1881    /* For future reference: Set errno on error, since we are called by
1882     * yylex_init()
1883     */
1884    return 0;
1885}
1886
1887/* yylex_destroy is for both reentrant and non-reentrant scanners. */
1888int yylex_destroy (yyscan_t yyscanner)
1889{
1890    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
1891
1892    /* Pop the buffer stack, destroying each element. */
1893    while(YY_CURRENT_BUFFER){
1894        yy_delete_buffer(YY_CURRENT_BUFFER ,yyscanner );
1895        YY_CURRENT_BUFFER_LVALUE = NULL;
1896        yypop_buffer_state(yyscanner);
1897    }
1898
1899    /* Destroy the stack itself. */
1900    yyfree(yyg->yy_buffer_stack ,yyscanner);
1901    yyg->yy_buffer_stack = NULL;
1902
1903    /* Destroy the start condition stack. */
1904        yyfree(yyg->yy_start_stack ,yyscanner );
1905        yyg->yy_start_stack = NULL;
1906
1907    /* Reset the globals. This is important in a non-reentrant scanner so the next time
1908     * yylex() is called, initialization will occur. */
1909    yy_init_globals( yyscanner);
1910
1911    /* Destroy the main struct (reentrant only). */
1912    yyfree ( yyscanner , yyscanner );
1913    yyscanner = NULL;
1914    return 0;
1915}
1916
1917/*
1918 * Internal utility routines.
1919 */
1920
1921#ifndef yytext_ptr
1922static void yy_flex_strncpy (char* s1, yyconst char * s2, int n , yyscan_t yyscanner)
1923{
1924    register int i;
1925    for ( i = 0; i < n; ++i )
1926        s1[i] = s2[i];
1927}
1928#endif
1929
1930#ifdef YY_NEED_STRLEN
1931static int yy_flex_strlen (yyconst char * s , yyscan_t yyscanner)
1932{
1933    register int n;
1934    for ( n = 0; s[n]; ++n )
1935        ;
1936
1937    return n;
1938}
1939#endif
1940
1941void *yyalloc (yy_size_t size , yyscan_t yyscanner)
1942{
1943    return (void *) malloc( size );
1944}
1945
1946void *yyrealloc (void * ptr, yy_size_t size , yyscan_t yyscanner)
1947{
1948    /* The cast to (char *) in the following accommodates both
1949     * implementations that use char* generic pointers, and those
1950     * that use void* generic pointers. It works with the latter
1951     * because both ANSI C and C++ allow castless assignment from
1952     * any pointer type to void*, and deal with argument conversions
1953     * as though doing an assignment.
1954     */
1955    return (void *) realloc( (char *) ptr, size );
1956}
1957
1958void yyfree (void * ptr , yyscan_t yyscanner)
1959{
1960    free( (char *) ptr ); /* see yyrealloc() for (char *) cast */
1961}
1962
1963#define YYTABLES_NAME "yytables"
1964
1965#line 53 "shell_lex.l"
shell_lex.l
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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%{
20#include "shell_internal.h"
21#include <string.h>
22#include <stdlib.h>
23
24static char *str_append(char *str, const char *src) {
25    if(str) {
26        size_t newlen = strlen(str) + strlen(src) + 1;
27
28        char *newstr = malloc(newlen);
29
30        strcpy(newstr, str);
31        strcat(newstr, src);
32
33        free(str);
34
35        return newstr;
36    } else
37        return strdup(src);
38}
39%}
40%option noyywrap nounput noinput batch reentrant
41
42%x STR
43%%
44["] { yyextra->strval = NULL; BEGIN(STR); }
45; return TOK_SEPARATOR;
46[[:space:]]+ return TOK_SPACE;
47[#] return TOK_COMMENT;
48[^[:space:];"#]+ { yyextra->strval = strdup(yytext); return TOK_STRING; }
49<STR>[^\\"]+ yyextra->strval = str_append(yyextra->strval, yytext);
50<STR>\\["] yyextra->strval = str_append(yyextra->strval, "\"");
51<STR>\\ yyextra->strval = str_append(yyextra->strval, "\\");
52<STR>["] { BEGIN(INITIAL); return TOK_STRING; }
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 "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
src/Makefile
1READLINE ?= 0
2
3
4ifneq (${READLINE},0)
5    LIBS += -lreadline
6    CPPFLAGS += -DWITH_READLINE
7endif
8
9CC = gcc
10TARGET = ../jzboot
11SOURCES = debug.c devmgr.c ingenic.c main.c shell_lex.c usbdev.c shell.c shell_builtins.c config.c spl_cmdset.c usbboot_cmdset.c
12CFLAGS = --std=gnu99 -Wall -Werror -I../include -O2 $(shell pkg-config libusb-1.0 --cflags) -Wunused-result
13LIBS += $(shell pkg-config libusb-1.0 --libs)
14
15OBJECTS = ${SOURCES:.c=.o}
16
17all: ${TARGET}
18
19${TARGET}: ${OBJECTS}
20    ${CC} ${LDFLAGS} -o $@ $^ ${LIBS}
21
22clean:
23    rm -f ${TARGET} ${OBJECTS} ${SOURCES:.c=.d}
24
25%.o: %.c
26    ${CC} ${CPPFLAGS} ${CFLAGS} -o $@ -MD -c $<
27
28%.c: %.l
29    flex -o $@ $<
30
31-include ${SOURCES:.c=.d}
src/config.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23
24#include "config.h"
25#include "debug.h"
26
27char **cfg_environ = NULL;
28
29static int env_size() {
30    int size = 0;
31
32    if(cfg_environ == NULL)
33        return 0;
34
35    for(int i = 0; cfg_environ[i] != NULL; i++)
36        size++;
37
38    return size;
39}
40
41char *cfg_getenv(const char *variable) {
42    if(cfg_environ == NULL)
43        return NULL;
44
45    size_t len = strlen(variable);
46
47    for(int i = 0; cfg_environ[i] != NULL; i++) {
48        char *str = cfg_environ[i], *sep = strchr(str, '=');
49
50        if(sep - str == len && memcmp(str, variable, len) == 0) {
51            return sep + 1;
52        }
53    }
54
55    return NULL;
56}
57
58void cfg_unsetenv(const char *variable) {
59    int size = env_size();
60
61    if(size == 0)
62        return;
63
64    size_t len = strlen(variable);
65
66    for(int i = 0; cfg_environ[i] != NULL; i++) {
67        char *str = cfg_environ[i], *sep = strchr(str, '=');
68
69        if(sep - str == len && memcmp(str, variable, len) == 0) {
70            free(str);
71
72            memcpy(cfg_environ + i, cfg_environ + i + 1, sizeof(char *) * (size - i));
73
74            cfg_environ = realloc(cfg_environ, sizeof(char *) * size);
75
76            return;
77        }
78    }
79}
80
81void cfg_setenv(const char *variable, const char *newval) {
82    int size = env_size();
83
84    size_t len = strlen(variable);
85
86    char *newstr = malloc(len + 1 + strlen(newval) + 1);
87
88    strcpy(newstr, variable);
89    strcat(newstr, "=");
90    strcat(newstr, newval);
91
92    if(size > 0) {
93        for(int i = 0; cfg_environ[i] != NULL; i++) {
94            char *str = cfg_environ[i], *sep = strchr(str, '=');
95
96            if(sep - str == len && memcmp(str, variable, len) == 0) {
97                free(str);
98
99                cfg_environ[i] = newstr;
100
101                return;
102            }
103        }
104    }
105
106    cfg_environ = realloc(cfg_environ, sizeof(char *) * (size + 2));
107
108    cfg_environ[size] = newstr;
109    cfg_environ[size + 1] = NULL;
110}
src/debug.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdarg.h>
20#include <stdio.h>
21
22#include "debug.h"
23
24static int debug_level = LEVEL_ERROR;
25
26void set_debug_level(int level) {
27    debug_level = level;
28}
29
30int get_debug_level() {
31    return debug_level;
32}
33
34void debug(int level, const char *fmt, ...) {
35    va_list list;
36
37    va_start(list, fmt);
38
39    if(level <= debug_level) {
40        if(level <= LEVEL_ERROR)
41            vfprintf(stderr, fmt, list);
42        else
43            vprintf(fmt, list);
44    }
45
46    va_end(list);
47}
src/devmgr.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <stdlib.h>
20
21#include "devmgr.h"
22#include "debug.h"
23
24typedef struct device {
25    struct device *next;
26    uint16_t vid;
27    uint16_t pid;
28    void *data;
29} usb_device_t;
30
31static const struct {
32    uint16_t vid;
33    uint16_t pid;
34} devices[] = {
35    { 0x601a, 0x4740 },
36    { 0x601a, 0x4750 },
37    { 0x601a, 0x4760 },
38    { 0, 0 }
39};
40
41static usb_device_t *list = NULL;
42
43int is_ingenic(uint16_t vid, uint16_t pid) {
44    for(int i = 0; devices[i].vid != 0; i++)
45        if(devices[i].vid == vid && devices[i].pid == pid)
46            return 1;
47
48    return 0;
49}
50
51void add_device(uint16_t vid, uint16_t pid, void *data) {
52    usb_device_t *newdev = malloc(sizeof(usb_device_t));
53
54    newdev->next = list;
55    newdev->vid = vid;
56    newdev->pid = pid;
57    newdev->data = data;
58
59    list = newdev;
60
61    debug(LEVEL_DEBUG, "Device manager: registered %04hX:%04hX with data %p\n", vid, pid, data);
62}
63
64void enum_devices(void (*handler)(int idx, uint16_t vid, uint16_t pid, void *data)) {
65    int idx = 0;
66
67    for(usb_device_t *dev = list; dev; dev = dev->next)
68        handler(idx++, dev->vid, dev->pid, dev->data);
69}
70
71void *get_device(int idx) {
72    for(usb_device_t *dev = list; dev; dev = dev->next)
73        if(idx-- == 0)
74            return dev->data;
75
76    return NULL;
77}
src/ingenic.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 <stdlib.h>
21#include <errno.h>
22#include <string.h>
23#include <stdio.h>
24#include <unistd.h>
25#include <ctype.h>
26
27#include "ingenic.h"
28#include "usbdev.h"
29#include "debug.h"
30#include "config.h"
31
32#define HANDLE ingenic_handle_t *handle = hndl
33#define BUILDTYPE(cmdset, id) (((cmdset) << 16) | (0x##id & 0xFFFF))
34#define CPUID(id) ((id) & 0xFFFF)
35#define CMDSET(id) (((id) & 0xFFFF0000) >> 16)
36
37#define CFGOPT(name, var, exp) { char *str = cfg_getenv(name); if(str == NULL) { debug(LEVEL_ERROR, "%s is not set\n", name); errno = EINVAL; return -1; }; int v = atoi(str); if(!(exp)) { debug(LEVEL_ERROR, "%s must be in %s\n", name, #exp); return -1; }; handle->cfg.var = v; }
38
39#define NOPT(name, var, exp) { char *str = cfg_getenv("NAND_" name); if(str == NULL) { debug(LEVEL_ERROR, "%s is not set\n", "NAND_" name); errno = EINVAL; return -1; }; int v = atoi(str); if(!(exp)) { debug(LEVEL_ERROR, "%s must be in %s\n", "NAND_" name, #exp); return -1; }; handle->nand.nand_##var = v; }
40
41#define CALLBACK(function, ...) if(handle->callbacks && handle->callbacks->function) handle->callbacks->function(__VA_ARGS__, handle->callbacks_data)
42
43typedef struct {
44    void *usb;
45    uint32_t type;
46    uint32_t total_sdram_size;
47
48    const ingenic_callbacks_t *callbacks;
49    void *callbacks_data;
50
51    firmware_config_t cfg;
52    nand_config_t nand;
53} ingenic_handle_t;
54
55static const struct {
56    const char * const magic;
57    uint32_t id;
58} magic_list[] = {
59    { "JZ4740V1", BUILDTYPE(CMDSET_SPL, 4740) },
60    { "JZ4750V1", BUILDTYPE(CMDSET_SPL, 4750) },
61    { "JZ4760V1", BUILDTYPE(CMDSET_SPL, 4760) },
62
63    { "Boot4740", BUILDTYPE(CMDSET_USBBOOT, 4740) },
64    { "Boot4750", BUILDTYPE(CMDSET_USBBOOT, 4750) },
65    { "Boot4760", BUILDTYPE(CMDSET_USBBOOT, 4760) },
66
67    { NULL, 0 }
68};
69
70static void hexdump(const void *data, size_t size) {
71        const unsigned char *bytes = data;
72
73        for(int i = 0; i < size; i+= 16) {
74                debug(LEVEL_DEBUG, "%04X ", i);
75
76                int chunk_size = size - i;
77        if(chunk_size > 16)
78            chunk_size = 16;
79
80                for(int j = 0; j < chunk_size; j++) {
81                        debug(LEVEL_DEBUG, "%02X ", bytes[i + j]);
82
83                        if(j == 7)
84                                debug(LEVEL_DEBUG, " ");
85                }
86
87                for(int j = 0; j < 16 - chunk_size; j++) {
88                        debug(LEVEL_DEBUG, " ");
89
90                        if(j == 8)
91                                debug(LEVEL_DEBUG, " ");
92                }
93
94                debug(LEVEL_DEBUG, "|");
95
96                for(int j = 0; j < chunk_size; j++) {
97                        debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.');
98                }
99
100                debug(LEVEL_DEBUG, "|\n");
101        }
102}
103
104static uint32_t ingenic_probe(void *usb_hndl) {
105    char magic[9];
106
107    if(usbdev_vendor(usb_hndl, USBDEV_FROMDEV, VR_GET_CPU_INFO, 0, 0, magic, 8) == -1)
108            return 0;
109
110    magic[8] = 0;
111
112    for(int i = 0; magic_list[i].magic != NULL; i++)
113        if(strcmp(magic_list[i].magic, magic) == 0)
114            return magic_list[i].id;
115
116    debug(LEVEL_ERROR, "Unknown CPU: '%s'\n", magic);
117    errno = EINVAL;
118    return 0;
119}
120
121void *ingenic_open(void *usb_hndl) {
122    uint32_t type = ingenic_probe(usb_hndl);
123
124    if(type == 0)
125        return NULL;
126
127    ingenic_handle_t *ret = malloc(sizeof(ingenic_handle_t));
128    ret->usb = usb_hndl;
129    ret->type = type;
130    ret->callbacks = NULL;
131
132    return ret;
133}
134
135int ingenic_redetect(void *hndl) {
136    HANDLE;
137
138    uint32_t type = ingenic_probe(handle->usb);
139
140    if(type == 0)
141        return -1;
142
143    uint32_t prev = handle->type;
144
145    handle->type = type;
146
147    if(CMDSET(prev) != CMDSET(type)) {
148        CALLBACK(cmdset_change, CMDSET(type));
149    }
150
151    return 0;
152}
153
154void ingenic_set_callbacks(void *hndl, const ingenic_callbacks_t *callbacks, void *arg) {
155    HANDLE;
156
157    handle->callbacks = callbacks;
158    handle->callbacks_data = arg;
159}
160
161int ingenic_cmdset(void *hndl) {
162    HANDLE;
163
164    return CMDSET(handle->type);
165}
166
167int ingenic_type(void *hndl) {
168    HANDLE;
169
170    return CPUID(handle->type);
171}
172
173void ingenic_close(void *hndl) {
174    HANDLE;
175
176    free(handle);
177}
178
179int ingenic_rebuild(void *hndl) {
180    HANDLE;
181
182    handle->cfg.cpu_id = CPUID(handle->type);
183
184    CFGOPT("EXTCLK", ext_clk, v <= 27 && v >= 12);
185    CFGOPT("CPUSPEED", cpu_speed, (v % 12) == 0);
186    handle->cfg.cpu_speed /= handle->cfg.ext_clk;
187    CFGOPT("PHMDIV", phm_div, v <= 32 && v >= 2);
188    CFGOPT("USEUART", use_uart, 1);
189    CFGOPT("BAUDRATE", baudrate, 1);
190
191    CFGOPT("SDRAM_BUSWIDTH", bus_width, (v == 16) || (v == 32));
192    handle->cfg.bus_width = handle->cfg.bus_width == 16 ? 1 : 0;
193    CFGOPT("SDRAM_BANKS", bank_num, (v >= 4) && ((v % 4) == 0));
194    handle->cfg.bank_num /= 4;
195    CFGOPT("SDRAM_ROWADDR", row_addr, 1);
196    CFGOPT("SDRAM_COLADDR", col_addr, 1);
197    CFGOPT("SDRAM_ISMOBILE", is_mobile, v == 0 || v == 1);
198    CFGOPT("SDRAM_ISBUSSHARE", is_busshare, v == 0 || v == 1);
199
200    memset(&handle->cfg.debug, 0, sizeof(ingenic_stage1_debug_t));
201
202    handle->total_sdram_size = (uint32_t)
203        (2 << (handle->cfg.row_addr + handle->cfg.col_addr - 1)) * 2
204        * (handle->cfg.bank_num + 1) * 2
205        * (2 - handle->cfg.bus_width);
206
207    handle->nand.cpuid = CPUID(handle->type);
208
209    NOPT("BUSWIDTH", bw, 1);
210    NOPT("ROWCYCLES", rc, 1);
211    NOPT("PAGESIZE", ps, 1);
212    NOPT("PAGEPERBLOCK", ppb, 1);
213    NOPT("FORCEERASE", force_erase, 1);
214// FIXME: pn is not set by xburst-tools usbboot. Is this intended?
215    NOPT("OOBSIZE", os, 1);
216    NOPT("ECCPOS", eccpos, 1);
217    NOPT("BADBLOCKPOS", bbpos, 1);
218    NOPT("BADBLOCKPAGE", bbpage, 1);
219    NOPT("PLANENUM", plane, 1);
220    NOPT("BCHBIT", bchbit, 1);
221    NOPT("WPPIN", wppin, 1);
222    NOPT("BLOCKPERCHIP", bpc, 1);
223
224    return 0;
225}
226
227uint32_t ingenic_sdram_size(void *hndl) {
228    HANDLE;
229
230    return handle->total_sdram_size;
231}
232
233static int ingenic_wordop(void *usb, int op, uint32_t base) {
234    return usbdev_vendor(usb, USBDEV_TODEV, op, (base >> 16), base & 0xFFFF, 0, 0);
235}
236
237int ingenic_stage1_debugop(void *hndl, const char *filename, uint32_t op, uint32_t pin, uint32_t base, uint32_t size) {
238    HANDLE;
239
240    handle->cfg.debug.debug_ops = op;
241    handle->cfg.debug.pin_num = pin;
242    handle->cfg.debug.start = base;
243    handle->cfg.debug.size = size;
244
245    int ret = ingenic_loadstage(handle, INGENIC_STAGE1, filename);
246
247    memset(&handle->cfg.debug, 0, sizeof(ingenic_stage1_debug_t));
248
249    return ret;
250}
251
252int ingenic_loadstage(void *hndl, int id, const char *file) {
253    HANDLE;
254
255    if(file == NULL) {
256        errno = EINVAL;
257
258        return -1;
259    }
260
261    uint32_t base;
262    int cmd;
263
264    switch(id) {
265    case INGENIC_STAGE1:
266        base = SDRAM_BASE + STAGE1_BASE;
267        cmd = VR_PROGRAM_START1;
268
269        break;
270
271    case INGENIC_STAGE2:
272        base = SDRAM_BASE + handle->total_sdram_size - STAGE2_CODESIZE;
273        cmd = VR_PROGRAM_START2;
274
275        break;
276
277    default:
278        errno = EINVAL;
279
280        return -1;
281    }
282
283    FILE *fd = fopen(file, "rb");
284
285    if(fd == NULL)
286        return -1;
287
288    fseek(fd, 0, SEEK_END);
289    int size = ftell(fd);
290    fseek(fd, 0, SEEK_SET);
291
292    void *data = malloc(size);
293    size_t read_bytes = fread(data, 1, size, fd);
294
295    fclose(fd);
296
297    if(read_bytes != size) {
298        free(data);
299
300        errno = EIO;
301
302        return -1;
303    }
304
305    memcpy(data + 8, &handle->cfg, sizeof(firmware_config_t));
306
307    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, base) == -1) {
308        free(data);
309
310        return -1;
311    }
312
313    int ret = usbdev_sendbulk(handle->usb, data, size);
314
315    free(data);
316
317    if(ret == -1)
318        return -1;
319
320    if(id == INGENIC_STAGE2) {
321        ret = usbdev_vendor(handle->usb, USBDEV_TODEV, VR_FLUSH_CACHES, 0, 0, 0, 0);
322
323        if(ret == -1)
324            return -1;
325    }
326
327    ret = usbdev_vendor(handle->usb, USBDEV_TODEV, cmd, (base >> 16), base & 0xFFFF, 0, 0);
328
329    if(ret == -1)
330        return -1;
331
332    usleep(250);
333
334    if(id == INGENIC_STAGE2)
335        return ingenic_redetect(hndl);
336    else
337        return 0;
338}
339
340int ingenic_memtest(void *hndl, const char *filename, uint32_t base, uint32_t size, uint32_t *fail) {
341    HANDLE;
342
343    int ret = ingenic_stage1_debugop(handle, filename, STAGE1_DEBUG_MEMTEST, 0, base, size);
344
345    if(ret == -1)
346        return -1;
347
348    uint32_t data[2];
349
350    ret = usbdev_recvbulk(handle->usb, &data, sizeof(data));
351
352    if(ret == -1)
353        return -1;
354
355    hexdump(data, ret);
356
357    if(ret < 4) {
358        errno = EIO;
359
360        return -1;
361    }
362
363    if(data[0] != 0) {
364        errno = EFAULT;
365
366        *fail = data[0];
367
368        return -1;
369    }
370
371    return 0;
372}
373
374int ingenic_configure_stage2(void *hndl) {
375    HANDLE;
376
377// DS_flash_info (nand_config_t only) is not implemented in stage2, so using DS_hand (nand_config_t + firmware_config_t)
378    uint8_t *hand = malloc(sizeof(nand_config_t) + sizeof(firmware_config_t));
379
380    memcpy(hand, &handle->nand, sizeof(nand_config_t));
381    memcpy(hand + sizeof(nand_config_t), &handle->cfg, sizeof(firmware_config_t));
382
383    int ret = usbdev_sendbulk(handle->usb, hand, sizeof(nand_config_t) + sizeof(firmware_config_t));
384
385    free(hand);
386
387    if(ret == -1)
388        return -1;
389
390    if(usbdev_vendor(handle->usb, USBDEV_TODEV, VR_CONFIGRATION, DS_hand, 0, 0, 0) == -1)
391        return -1;
392
393    uint32_t result[8];
394
395    ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
396
397    if(ret == -1)
398        return -1;
399
400    return 0;
401}
402
403int ingenic_load_sdram(void *hndl, void *data, uint32_t base, uint32_t size) {
404    HANDLE;
405
406    int max = size, value = 0;
407
408    CALLBACK(progress, PROGRESS_INIT, 0, max);
409
410    while(size) {
411        int block = size > STAGE2_IOBUF ? STAGE2_IOBUF : size;
412
413        debug(LEVEL_DEBUG, "Loading %d bytes from %p to 0x%08X\n", block, data, base);
414
415        if(usbdev_sendbulk(handle->usb, data, block) == -1)
416            return -1;
417
418        if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, base) == -1)
419            return -1;
420
421        if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, block) == -1)
422            return -1;
423
424
425        if(usbdev_vendor(handle->usb, USBDEV_TODEV, VR_SDRAM_OPS, SDRAM_LOAD, 0, 0, 0) == -1)
426            return -1;
427
428        uint32_t result[8];
429
430        int ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
431        if(ret == -1)
432            return -1;
433
434        hexdump(result, ret);
435
436        data += block;
437        base += block;
438        size -= block;
439        value += block;
440
441        CALLBACK(progress, PROGRESS_UPDATE, value, max);
442
443    }
444
445    CALLBACK(progress, PROGRESS_FINI, 0, 0);
446
447    debug(LEVEL_DEBUG, "Load done\n");
448
449    return 0;
450}
451
452int ingenic_load_sdram_file(void *hndl, uint32_t base, const char *file) {
453    HANDLE;
454
455    FILE *fd = fopen(file, "rb");
456
457    if(fd == NULL)
458        return -1;
459
460    fseek(fd, 0, SEEK_END);
461    int size = ftell(fd);
462    fseek(fd, 0, SEEK_SET);
463
464    void *data = malloc(size);
465    size_t bytes = fread(data, 1, size, fd);
466
467    fclose(fd);
468
469    if(bytes != size) {
470        free(data);
471
472        errno = EIO;
473
474        return -1;
475    }
476
477    int ret = ingenic_load_sdram(handle, data, base, size);
478
479    free(data);
480
481    return ret;
482}
483
484int ingenic_go(void *hndl, uint32_t address) {
485    HANDLE;
486
487    debug(LEVEL_DEBUG, "Go to 0x%08X\n", address);
488
489    return ingenic_wordop(handle->usb, VR_PROGRAM_START2, address);
490}
491
492static inline int ingenic_nandop(void *usb, uint8_t cs, uint8_t request, uint8_t param) {
493    return usbdev_vendor(usb, USBDEV_TODEV, VR_NAND_OPS, (param << 12) | (cs << 4) | (request & 0x0F), 0, 0, 0);
494}
495
496int ingenic_query_nand(void *hndl, int cs, nand_info_t *info) {
497    HANDLE;
498
499    if(ingenic_nandop(handle->usb, cs, NAND_QUERY, 0) == -1)
500        return -1;
501
502    uint32_t dummy[8];
503
504    int ret = usbdev_recvbulk(handle->usb, dummy, sizeof(dummy));
505
506    if(ret == -1)
507        return -1;
508
509    if(ret < sizeof(nand_info_t)) {
510        errno = EIO;
511
512        return -1;
513    }
514
515    memcpy(info, dummy, sizeof(nand_info_t));
516
517    if(usbdev_recvbulk(handle->usb, dummy, sizeof(dummy)) == -1)
518        return -1;
519
520    return 0;
521}
522
523int ingenic_dump_nand(void *hndl, int cs, int start, int pages, int type, const char *filename) {
524    HANDLE;
525
526    int raw = type & NAND_RAW;
527    type &= ~NAND_RAW;
528
529    int page_size = (handle->nand.nand_ps + (type == NO_OOB ? 0 : handle->nand.nand_os));
530    int chunk_pages = STAGE2_IOBUF / page_size;
531
532    FILE *dest = fopen(filename, "wb");
533
534    if(dest == NULL)
535        return -1;
536
537    void *iobuf = malloc(chunk_pages * page_size);
538
539    int ret = 0;
540    int value = 0, max = pages;
541
542    CALLBACK(progress, PROGRESS_INIT, 0, max);
543
544    while(pages > 0) {
545        int chunk = pages < chunk_pages ? pages : chunk_pages;
546        int bytes = chunk * page_size;
547
548        ret = ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start);
549
550        if(ret == -1)
551            break;
552
553        ret = ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, chunk);
554
555        if(ret == -1)
556            break;
557
558        ret = ingenic_nandop(handle->usb, cs, raw ? NAND_READ_RAW : NAND_READ, type);
559
560        if(ret == -1)
561            break;
562
563        int ret = usbdev_recvbulk(handle->usb, iobuf, bytes);
564
565        if(ret == -1)
566            return -1;
567
568        if(ret != bytes) {
569            debug(LEVEL_ERROR, "Ingenic: NAND dump truncated: expected %d bytes, received %d\n", bytes, ret);
570
571            errno = EIO;
572
573            break;
574        }
575
576        uint16_t result[4];
577
578
579        ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
580
581        if(ret == -1)
582            return -1;
583
584        if(result[3] != 0 && !raw) {
585            debug(LEVEL_ERROR, "Ingenic: ECC failure while reading NAND. See UART output for details\n");
586
587            errno = EIO;
588
589            break;
590        }
591
592        fwrite(iobuf, bytes, 1, dest);
593
594        start += chunk;
595        pages -= chunk;
596
597        value += chunk;
598
599        CALLBACK(progress, PROGRESS_UPDATE, value, max);
600    }
601
602    free(iobuf);
603    fclose(dest);
604
605    CALLBACK(progress, PROGRESS_FINI, 0, 0);
606
607    return ret;
608}
609
610int ingenic_erase_nand(void *hndl, int cs, int start, int blocks) {
611    HANDLE;
612
613    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start) == -1)
614        return -1;
615
616    if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, blocks) == -1)
617        return -1;
618
619    if(ingenic_nandop(handle->usb, cs, NAND_ERASE, 0) == -1)
620        return -1;
621
622    uint16_t result[4];
623
624    int ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
625
626    if(ret == -1)
627        return -1;
628
629    hexdump(result, ret);
630
631    return 0;
632}
633
634int ingenic_program_nand(void *hndl, int cs, int start, int type, const char *filename) {
635    HANDLE;
636
637    int page_size = (handle->nand.nand_ps + (type == NO_OOB ? 0 : handle->nand.nand_os));
638    int chunk_pages = STAGE2_IOBUF / page_size;
639
640    FILE *in = fopen(filename, "rb");
641
642    if(in == NULL)
643        return -1;
644
645    fseek(in, 0, SEEK_END);
646    int file_size = ftell(in);
647    fseek(in, 0, SEEK_SET);
648
649    int tail = file_size % page_size;
650
651    if(tail) {
652        tail = page_size - tail;
653
654        file_size += tail;
655    }
656
657    int pages = file_size / page_size;
658    int ret = 0;
659
660    void *iobuf = malloc(chunk_pages * page_size);
661
662    debug(LEVEL_INFO, "Programming %d pages from %d (%d bytes, %d bytes/page)\n", pages, start, file_size, page_size);
663
664    int value = 0, max = pages;
665
666    CALLBACK(progress, PROGRESS_INIT, 0, max);
667
668    while(pages > 0) {
669        int chunk = pages < chunk_pages ? pages : chunk_pages;
670        int bytes = chunk * page_size;
671
672        ret = fread(iobuf, 1, bytes, in);
673
674        if(pages < chunk_pages && tail) {
675            memset(iobuf + ret, 0xFF, tail);
676
677            ret += tail;
678        }
679
680        if(ret != bytes) {
681            debug(LEVEL_ERROR, "fread: %d\n", ret);
682
683            ret = -1;
684            errno = EIO;
685
686            break;
687        }
688
689        if(usbdev_sendbulk(handle->usb, iobuf, bytes) == -1)
690            return -1;
691
692        ret = ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start);
693
694        if(ret == -1)
695            break;
696
697        ret = ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, chunk);
698
699        if(ret == -1)
700            break;
701
702        ret = ingenic_nandop(handle->usb, cs, NAND_PROGRAM, type);
703
704        if(ret == -1)
705            break;
706
707        uint16_t result[4];
708
709        ret = usbdev_recvbulk(handle->usb, result, sizeof(result));
710
711        if(ret == -1)
712            return -1;
713
714        hexdump(result, ret);
715
716        start += chunk;
717        pages -= chunk;
718        value += chunk;
719
720        CALLBACK(progress, PROGRESS_UPDATE, value, max);
721    }
722
723    free(iobuf);
724    fclose(in);
725
726    CALLBACK(progress, PROGRESS_FINI, 0, 0);
727
728    return ret;
729}
730
731int ingenic_load_nand(void *hndl, int cs, int start, int pages, uint32_t base) {
732    HANDLE;
733
734    if(ingenic_wordop(handle->usb, VR_PROGRAM_START1, base) == -1)
735        return -1;
736
737    if(ingenic_wordop(handle->usb, VR_SET_DATA_ADDRESS, start) == -1)
738        return -1;
739
740    if(ingenic_wordop(handle->usb, VR_SET_DATA_LENGTH, pages) == -1)
741        return -1;
742
743    if(ingenic_nandop(handle->usb, cs, NAND_READ_TO_RAM, 0) == -1)
744        return -1;
745
746    uint16_t result[4];
747
748    if(usbdev_recvbulk(handle->usb, result, sizeof(result)) == -1)
749        return -1;
750
751    return 0;
752}
753
src/main.c
1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
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 <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include "usbdev.h"
23#include "debug.h"
24#include "devmgr.h"
25#include "ingenic.h"
26#include "shell.h"
27
28static void usage(const char *app) {
29    printf(
30        "Usage: \n"
31        " Enumeration: %1$s -e\n"
32                " Interactive mode: %1$s -i <INDEX>\n"
33                " Batch mode (with script): %1$s -i <INDEX> -b <FILE>\n"
34                " Batch mode (command list): %1$s -i <INDEX> -c <COMMAND>\n\n"
35
36        "USB loader tool for Ingenic Semiconductor XBurst-based SoC\n\n"
37        " -e Enumerate devices only\n"
38        " -i <INDEX> Open device with index INDEX in interactive or batch mode\n"
39        " -c <CMD> Run semicolon-separated commands and exit\n"
40                " -d <DEBUG> Set output level (0 - no reporting, 4 - max reporting), default = 1 (errors only)\n"
41                " -C <FILE> Execute configuration script FILE before anything else\n"
42        " -b <FILE> Execute script in FILE\n\n", app);
43}
44
45static void dev_handler(int idx, uint16_t vid, uint16_t pid, void *data) {
46    printf(" Device %d: %04hX:%04hX\n", idx, vid, pid);
47}
48
49int main(int argc, char *argv[]) {
50    int ch;
51    int idx = -1, enumerate = 0;
52    char *cmd = NULL, *script = NULL, *config = NULL;
53
54    while((ch = getopt(argc, argv, "b:i:ec:d:C:")) != -1) {
55        switch(ch) {
56        case 'e':
57            enumerate = 1;
58
59            break;
60
61        case 'i':
62            idx = atoi(optarg);
63
64            break;
65
66        case 'c':
67            cmd = optarg;
68
69            break;
70
71        case 'b':
72            script = optarg;
73
74            break;
75
76        case 'C':
77            config = optarg;
78
79            break;
80
81        case 'd':
82            set_debug_level(atoi(optarg));
83
84            break;
85
86        default:
87            usage(argv[0]);
88
89            return 1;
90        }
91    }
92
93    if(!enumerate && idx < 0) {
94        usage(argv[0]);
95
96        return 1;
97    }
98
99    if(usbdev_init() == -1) {
100        perror("usbdev_init");
101
102        return 1;
103    }
104
105    atexit(usbdev_fini);
106
107    if(usbdev_enumerate() == -1) {
108        perror("usbdev_enumerate");
109
110        return 1;
111    }
112
113    if(enumerate) {
114        printf("Ingenic devices list:\n");
115
116        enum_devices(dev_handler);
117
118        return 0;
119    }
120
121    void *data = get_device(idx);
122
123    if(data == NULL) {
124        fprintf(stderr, "Device with index %d not found\n", idx);
125
126        return 1;
127    }
128
129    void *hndl = usbdev_open(data);
130
131    if(hndl == NULL) {
132        perror("usbdev_open");
133
134        return 1;
135    }
136
137    int ret = 0;
138
139    void *ingenic = ingenic_open(hndl);
140
141    if(ingenic == NULL) {
142        perror("ingenic_open");
143
144        ret = 1;
145
146        goto exit_usb;
147    }
148
149    shell_context_t *shell = shell_init(ingenic);
150
151    if(shell == NULL) {
152        perror("shell_init");
153
154        ret = 1;
155
156        goto exit_ingenic;
157    }
158
159    if(config) {
160        if(shell_source(shell, config) == -1) {
161            perror("shell_source");
162
163            ret = 1;
164
165            goto exit_shell;
166        }
167    }
168
169    if(cmd != NULL) {
170        if(shell_execute(shell, cmd) == -1) {
171            perror("shell_execute");
172
173            ret = 1;
174        }
175
176    } else if(script != NULL) {
177        if(shell_source(shell, script) == -1) {
178            perror("shell_source");
179
180            ret = 1;
181        }
182    } else
183        shell_interactive(shell);
184
185exit_shell:
186    shell_fini(shell);
187
188exit_ingenic:
189    ingenic_close(ingenic);
190
191exit_usb:
192    usbdev_close(hndl);
193
194    return ret;
195}
src/shell.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#ifdef WITH_READLINE
23#include <readline/readline.h>
24#include <readline/history.h>
25#endif
26#include <stdlib.h>
27#include <unistd.h>
28#include <errno.h>
29#include <sys/ioctl.h>
30
31#include "shell_internal.h"
32#include "debug.h"
33#include "ingenic.h"
34
35static void shell_update_cmdset(uint32_t cmdset, void *arg);
36static void shell_progress(int action, int value, int max, void *arg);
37
38static const ingenic_callbacks_t shell_callbacks = {
39    shell_update_cmdset,
40    shell_progress
41};
42
43static const struct {
44    int set;
45    const char *name;
46    const shell_command_t *commands;
47} cmdsets[] = {
48    { CMDSET_SPL, "SPL", spl_cmdset },
49    { CMDSET_USBBOOT, "USBBoot", usbboot_cmdset },
50    { 0, NULL, NULL }
51};
52
53shell_context_t *shell_init(void *ingenic) {
54#ifdef WITH_READLINE
55    rl_initialize();
56#endif
57
58    debug(LEVEL_DEBUG, "Initializing shell\n");
59
60    shell_context_t *ctx = malloc(sizeof(shell_context_t));
61    memset(ctx, 0, sizeof(shell_context_t));
62    ctx->device = ingenic;
63
64    ingenic_set_callbacks(ingenic, &shell_callbacks, ctx);
65
66    shell_update_cmdset(ingenic_cmdset(ingenic), ctx);
67
68    return ctx;
69}
70
71int shell_enumerate_commands(shell_context_t *ctx, int (*callback)(shell_context_t *ctx, const shell_command_t *cmd, void *arg), void *arg) {
72    for(int i = 0; builtin_cmdset[i].cmd != NULL; i++) {
73        int ret = callback(ctx, builtin_cmdset + i, arg);
74
75        if(ret != 0)
76            return ret;
77    }
78
79    if(ctx->set_cmds)
80        for(int i = 0; ctx->set_cmds[i].cmd != NULL; i++) {
81            int ret = callback(ctx, ctx->set_cmds + i, arg);
82
83            if(ret != 0)
84                return ret;
85        }
86
87    return 0;
88}
89
90static int shell_run_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) {
91    shell_run_data_t *data = arg;
92
93    if(strcmp(cmd->cmd, data->argv[0]) == 0) {
94        int invalid = 0;
95
96        if(cmd->args == NULL && data->argc != 1)
97            invalid = 1;
98        else if(cmd->args) {
99            char *dup = strdup(cmd->args), *save = dup, *ptrptr = NULL;
100
101            int pos = 1;
102            int max_tokens = 1;
103
104            for(char *token = strtok_r(dup, " ", &ptrptr); token; token = strtok_r(NULL, " ", &ptrptr)) {
105                if(strcmp(token, "...") == 0) {
106                    max_tokens = -1;
107
108                    break;
109                }
110
111                max_tokens++;
112
113                if(data->argc - 1 < pos) {
114                    if(*token == '[') {
115                        break;
116                    } else if(*token == '<') {
117                        invalid = 1;
118
119                        break;
120                    }
121                }
122
123                pos++;
124            }
125
126            if(max_tokens != -1 && data->argc > max_tokens)
127                invalid = 1;
128
129
130            free(save);
131        }
132
133        if(invalid) {
134            if(cmd->args)
135                fprintf(stderr, "Usage: %s %s\n", cmd->cmd, cmd->args);
136            else
137                fprintf(stderr, "Usage: %s\n", cmd->cmd);
138
139            errno = EINVAL;
140
141            return -1;
142
143        } else {
144            int ret = cmd->handler(ctx, data->argc, data->argv);
145
146            if(ret == 0)
147                return 1;
148            else
149                return ret;
150        }
151    } else