Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 2 | * SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 3 | * |
| 4 | * based on: cmd_jffs2.c |
| 5 | * |
| 6 | * Add support for a CRAMFS located in RAM |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | /* |
| 11 | * CRAMFS support |
| 12 | */ |
| 13 | #include <common.h> |
| 14 | #include <command.h> |
| 15 | #include <malloc.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <jffs2/jffs2.h> |
| 19 | #include <jffs2/load_kernel.h> |
| 20 | #include <cramfs/cramfs_fs.h> |
| 21 | |
| 22 | /* enable/disable debugging messages */ |
| 23 | #define DEBUG_CRAMFS |
| 24 | #undef DEBUG_CRAMFS |
| 25 | |
| 26 | #ifdef DEBUG_CRAMFS |
| 27 | # define DEBUGF(fmt, args...) printf(fmt ,##args) |
| 28 | #else |
| 29 | # define DEBUGF(fmt, args...) |
| 30 | #endif |
| 31 | |
Heiko Schocher | ee8bc96 | 2011-05-03 02:15:01 +0000 | [diff] [blame] | 32 | #include <flash.h> |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 33 | |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 34 | #ifndef CONFIG_MTD_NOR_FLASH |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 35 | # define OFFSET_ADJUSTMENT 0 |
| 36 | #else |
| 37 | # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0]) |
| 38 | #endif |
| 39 | |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 40 | #ifndef CONFIG_CMD_JFFS2 |
| 41 | #include <linux/stat.h> |
| 42 | char *mkmodestr(unsigned long mode, char *str) |
| 43 | { |
| 44 | static const char *l = "xwr"; |
| 45 | int mask = 1, i; |
| 46 | char c; |
| 47 | |
| 48 | switch (mode & S_IFMT) { |
| 49 | case S_IFDIR: str[0] = 'd'; break; |
| 50 | case S_IFBLK: str[0] = 'b'; break; |
| 51 | case S_IFCHR: str[0] = 'c'; break; |
| 52 | case S_IFIFO: str[0] = 'f'; break; |
| 53 | case S_IFLNK: str[0] = 'l'; break; |
| 54 | case S_IFSOCK: str[0] = 's'; break; |
| 55 | case S_IFREG: str[0] = '-'; break; |
| 56 | default: str[0] = '?'; |
| 57 | } |
| 58 | |
| 59 | for(i = 0; i < 9; i++) { |
| 60 | c = l[i%3]; |
| 61 | str[9-i] = (mode & mask)?c:'-'; |
| 62 | mask = mask<<1; |
| 63 | } |
| 64 | |
| 65 | if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; |
| 66 | if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; |
| 67 | if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; |
| 68 | str[10] = '\0'; |
| 69 | return str; |
| 70 | } |
| 71 | #endif /* CONFIG_CMD_JFFS2 */ |
| 72 | |
| 73 | extern int cramfs_check (struct part_info *info); |
| 74 | extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename); |
| 75 | extern int cramfs_ls (struct part_info *info, char *filename); |
| 76 | extern int cramfs_info (struct part_info *info); |
| 77 | |
| 78 | /***************************************************/ |
Bin Meng | a187559 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 79 | /* U-Boot commands */ |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 80 | /***************************************************/ |
| 81 | |
| 82 | /** |
| 83 | * Routine implementing fsload u-boot command. This routine tries to load |
| 84 | * a requested file from cramfs filesystem at location 'cramfsaddr'. |
| 85 | * cramfsaddr is an evironment variable. |
| 86 | * |
| 87 | * @param cmdtp command internal data |
| 88 | * @param flag command flag |
| 89 | * @param argc number of arguments supplied to the command |
| 90 | * @param argv arguments list |
| 91 | * @return 0 on success, 1 otherwise |
| 92 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 93 | int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 94 | { |
| 95 | char *filename; |
| 96 | int size; |
| 97 | ulong offset = load_addr; |
| 98 | |
| 99 | struct part_info part; |
| 100 | struct mtd_device dev; |
| 101 | struct mtdids id; |
| 102 | |
| 103 | ulong addr; |
| 104 | addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16); |
| 105 | |
| 106 | /* hack! */ |
| 107 | /* cramfs_* only supports NOR flash chips */ |
| 108 | /* fake the device type */ |
| 109 | id.type = MTD_DEV_TYPE_NOR; |
| 110 | id.num = 0; |
| 111 | dev.id = &id; |
| 112 | part.dev = &dev; |
| 113 | /* fake the address offset */ |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 114 | part.offset = addr - OFFSET_ADJUSTMENT; |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 115 | |
| 116 | /* pre-set Boot file name */ |
| 117 | if ((filename = getenv("bootfile")) == NULL) { |
| 118 | filename = "uImage"; |
| 119 | } |
| 120 | |
| 121 | if (argc == 2) { |
| 122 | filename = argv[1]; |
| 123 | } |
| 124 | if (argc == 3) { |
| 125 | offset = simple_strtoul(argv[1], NULL, 0); |
| 126 | load_addr = offset; |
| 127 | filename = argv[2]; |
| 128 | } |
| 129 | |
| 130 | size = 0; |
| 131 | if (cramfs_check(&part)) |
| 132 | size = cramfs_load ((char *) offset, &part, filename); |
| 133 | |
| 134 | if (size > 0) { |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 135 | printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n", |
| 136 | size, offset); |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 137 | setenv_hex("filesize", size); |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 138 | } else { |
| 139 | printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename); |
| 140 | } |
| 141 | |
| 142 | return !(size > 0); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Routine implementing u-boot ls command which lists content of a given |
| 147 | * directory at location 'cramfsaddr'. |
| 148 | * cramfsaddr is an evironment variable. |
| 149 | * |
| 150 | * @param cmdtp command internal data |
| 151 | * @param flag command flag |
| 152 | * @param argc number of arguments supplied to the command |
| 153 | * @param argv arguments list |
| 154 | * @return 0 on success, 1 otherwise |
| 155 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 156 | int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 157 | { |
| 158 | char *filename = "/"; |
| 159 | int ret; |
| 160 | struct part_info part; |
| 161 | struct mtd_device dev; |
| 162 | struct mtdids id; |
| 163 | |
| 164 | ulong addr; |
| 165 | addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16); |
| 166 | |
| 167 | /* hack! */ |
| 168 | /* cramfs_* only supports NOR flash chips */ |
| 169 | /* fake the device type */ |
| 170 | id.type = MTD_DEV_TYPE_NOR; |
| 171 | id.num = 0; |
| 172 | dev.id = &id; |
| 173 | part.dev = &dev; |
| 174 | /* fake the address offset */ |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 175 | part.offset = addr - OFFSET_ADJUSTMENT; |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 176 | |
| 177 | if (argc == 2) |
| 178 | filename = argv[1]; |
| 179 | |
| 180 | ret = 0; |
| 181 | if (cramfs_check(&part)) |
| 182 | ret = cramfs_ls (&part, filename); |
| 183 | |
| 184 | return ret ? 0 : 1; |
| 185 | } |
| 186 | |
| 187 | /* command line only */ |
| 188 | |
| 189 | /***************************************************/ |
| 190 | U_BOOT_CMD( |
| 191 | cramfsload, 3, 0, do_cramfs_load, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 192 | "load binary file from a filesystem image", |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 193 | "[ off ] [ filename ]\n" |
| 194 | " - load binary file from address 'cramfsaddr'\n" |
| 195 | " with offset 'off'\n" |
| 196 | ); |
| 197 | U_BOOT_CMD( |
| 198 | cramfsls, 2, 1, do_cramfs_ls, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 199 | "list files in a directory (default /)", |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 200 | "[ directory ]\n" |
| 201 | " - list files in a directory.\n" |
| 202 | ); |