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