Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License as |
| 4 | * published by the Free Software Foundation; either version 2 of |
| 5 | * the License, or (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 15 | * MA 02111-1307 USA |
| 16 | * |
| 17 | * based on: cmd_jffs2.c |
| 18 | * |
| 19 | * Add support for a CRAMFS located in RAM |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * CRAMFS support |
| 25 | */ |
| 26 | #include <common.h> |
| 27 | #include <command.h> |
| 28 | #include <malloc.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/ctype.h> |
| 31 | #include <jffs2/jffs2.h> |
| 32 | #include <jffs2/load_kernel.h> |
| 33 | #include <cramfs/cramfs_fs.h> |
| 34 | |
| 35 | /* enable/disable debugging messages */ |
| 36 | #define DEBUG_CRAMFS |
| 37 | #undef DEBUG_CRAMFS |
| 38 | |
| 39 | #ifdef DEBUG_CRAMFS |
| 40 | # define DEBUGF(fmt, args...) printf(fmt ,##args) |
| 41 | #else |
| 42 | # define DEBUGF(fmt, args...) |
| 43 | #endif |
| 44 | |
| 45 | #ifdef CONFIG_CRAMFS_CMDLINE |
Heiko Schocher | ee8bc96 | 2011-05-03 02:15:01 +0000 | [diff] [blame] | 46 | #include <flash.h> |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 47 | |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 48 | #ifdef CONFIG_SYS_NO_FLASH |
| 49 | # define OFFSET_ADJUSTMENT 0 |
| 50 | #else |
| 51 | # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0]) |
| 52 | #endif |
| 53 | |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 54 | #ifndef CONFIG_CMD_JFFS2 |
| 55 | #include <linux/stat.h> |
| 56 | char *mkmodestr(unsigned long mode, char *str) |
| 57 | { |
| 58 | static const char *l = "xwr"; |
| 59 | int mask = 1, i; |
| 60 | char c; |
| 61 | |
| 62 | switch (mode & S_IFMT) { |
| 63 | case S_IFDIR: str[0] = 'd'; break; |
| 64 | case S_IFBLK: str[0] = 'b'; break; |
| 65 | case S_IFCHR: str[0] = 'c'; break; |
| 66 | case S_IFIFO: str[0] = 'f'; break; |
| 67 | case S_IFLNK: str[0] = 'l'; break; |
| 68 | case S_IFSOCK: str[0] = 's'; break; |
| 69 | case S_IFREG: str[0] = '-'; break; |
| 70 | default: str[0] = '?'; |
| 71 | } |
| 72 | |
| 73 | for(i = 0; i < 9; i++) { |
| 74 | c = l[i%3]; |
| 75 | str[9-i] = (mode & mask)?c:'-'; |
| 76 | mask = mask<<1; |
| 77 | } |
| 78 | |
| 79 | if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; |
| 80 | if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; |
| 81 | if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; |
| 82 | str[10] = '\0'; |
| 83 | return str; |
| 84 | } |
| 85 | #endif /* CONFIG_CMD_JFFS2 */ |
| 86 | |
| 87 | extern int cramfs_check (struct part_info *info); |
| 88 | extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename); |
| 89 | extern int cramfs_ls (struct part_info *info, char *filename); |
| 90 | extern int cramfs_info (struct part_info *info); |
| 91 | |
| 92 | /***************************************************/ |
| 93 | /* U-boot commands */ |
| 94 | /***************************************************/ |
| 95 | |
| 96 | /** |
| 97 | * Routine implementing fsload u-boot command. This routine tries to load |
| 98 | * a requested file from cramfs filesystem at location 'cramfsaddr'. |
| 99 | * cramfsaddr is an evironment variable. |
| 100 | * |
| 101 | * @param cmdtp command internal data |
| 102 | * @param flag command flag |
| 103 | * @param argc number of arguments supplied to the command |
| 104 | * @param argv arguments list |
| 105 | * @return 0 on success, 1 otherwise |
| 106 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 107 | 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] | 108 | { |
| 109 | char *filename; |
| 110 | int size; |
| 111 | ulong offset = load_addr; |
| 112 | |
| 113 | struct part_info part; |
| 114 | struct mtd_device dev; |
| 115 | struct mtdids id; |
| 116 | |
| 117 | ulong addr; |
| 118 | addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16); |
| 119 | |
| 120 | /* hack! */ |
| 121 | /* cramfs_* only supports NOR flash chips */ |
| 122 | /* fake the device type */ |
| 123 | id.type = MTD_DEV_TYPE_NOR; |
| 124 | id.num = 0; |
| 125 | dev.id = &id; |
| 126 | part.dev = &dev; |
| 127 | /* fake the address offset */ |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 128 | part.offset = addr - OFFSET_ADJUSTMENT; |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 129 | |
| 130 | /* pre-set Boot file name */ |
| 131 | if ((filename = getenv("bootfile")) == NULL) { |
| 132 | filename = "uImage"; |
| 133 | } |
| 134 | |
| 135 | if (argc == 2) { |
| 136 | filename = argv[1]; |
| 137 | } |
| 138 | if (argc == 3) { |
| 139 | offset = simple_strtoul(argv[1], NULL, 0); |
| 140 | load_addr = offset; |
| 141 | filename = argv[2]; |
| 142 | } |
| 143 | |
| 144 | size = 0; |
| 145 | if (cramfs_check(&part)) |
| 146 | size = cramfs_load ((char *) offset, &part, filename); |
| 147 | |
| 148 | if (size > 0) { |
| 149 | char buf[10]; |
| 150 | printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n", |
| 151 | size, offset); |
| 152 | sprintf(buf, "%x", size); |
| 153 | setenv("filesize", buf); |
| 154 | } else { |
| 155 | printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename); |
| 156 | } |
| 157 | |
| 158 | return !(size > 0); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Routine implementing u-boot ls command which lists content of a given |
| 163 | * directory at location 'cramfsaddr'. |
| 164 | * cramfsaddr is an evironment variable. |
| 165 | * |
| 166 | * @param cmdtp command internal data |
| 167 | * @param flag command flag |
| 168 | * @param argc number of arguments supplied to the command |
| 169 | * @param argv arguments list |
| 170 | * @return 0 on success, 1 otherwise |
| 171 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 172 | 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] | 173 | { |
| 174 | char *filename = "/"; |
| 175 | int ret; |
| 176 | struct part_info part; |
| 177 | struct mtd_device dev; |
| 178 | struct mtdids id; |
| 179 | |
| 180 | ulong addr; |
| 181 | addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16); |
| 182 | |
| 183 | /* hack! */ |
| 184 | /* cramfs_* only supports NOR flash chips */ |
| 185 | /* fake the device type */ |
| 186 | id.type = MTD_DEV_TYPE_NOR; |
| 187 | id.num = 0; |
| 188 | dev.id = &id; |
| 189 | part.dev = &dev; |
| 190 | /* fake the address offset */ |
Heiko Schocher | 62a813b | 2011-05-03 02:15:15 +0000 | [diff] [blame] | 191 | part.offset = addr - OFFSET_ADJUSTMENT; |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 192 | |
| 193 | if (argc == 2) |
| 194 | filename = argv[1]; |
| 195 | |
| 196 | ret = 0; |
| 197 | if (cramfs_check(&part)) |
| 198 | ret = cramfs_ls (&part, filename); |
| 199 | |
| 200 | return ret ? 0 : 1; |
| 201 | } |
| 202 | |
| 203 | /* command line only */ |
| 204 | |
| 205 | /***************************************************/ |
| 206 | U_BOOT_CMD( |
| 207 | cramfsload, 3, 0, do_cramfs_load, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 208 | "load binary file from a filesystem image", |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 209 | "[ off ] [ filename ]\n" |
| 210 | " - load binary file from address 'cramfsaddr'\n" |
| 211 | " with offset 'off'\n" |
| 212 | ); |
| 213 | U_BOOT_CMD( |
| 214 | cramfsls, 2, 1, do_cramfs_ls, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 215 | "list files in a directory (default /)", |
Heiko Schocher | 88ffb26 | 2010-01-07 08:55:54 +0100 | [diff] [blame] | 216 | "[ directory ]\n" |
| 217 | " - list files in a directory.\n" |
| 218 | ); |
| 219 | |
| 220 | #endif /* #ifdef CONFIG_CRAMFS_CMDLINE */ |
| 221 | |
| 222 | /***************************************************/ |