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