blob: c1034916c600bda104df6073e75a52ae4ec55679 [file] [log] [blame]
Heiko Schocher88ffb262010-01-07 08:55:54 +01001/*
Wolfgang Denk1a459662013-07-08 09:37:19 +02002 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher88ffb262010-01-07 08:55:54 +01003 *
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 Schocheree8bc962011-05-03 02:15:01 +000032#include <flash.h>
Heiko Schocher88ffb262010-01-07 08:55:54 +010033
Heiko Schocher62a813b2011-05-03 02:15:15 +000034#ifdef CONFIG_SYS_NO_FLASH
35# define OFFSET_ADJUSTMENT 0
36#else
37# define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
38#endif
39
Heiko Schocher88ffb262010-01-07 08:55:54 +010040#ifndef CONFIG_CMD_JFFS2
41#include <linux/stat.h>
42char *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
73extern int cramfs_check (struct part_info *info);
74extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
75extern int cramfs_ls (struct part_info *info, char *filename);
76extern int cramfs_info (struct part_info *info);
77
78/***************************************************/
Bin Menga1875592016-02-05 19:30:11 -080079/* U-Boot commands */
Heiko Schocher88ffb262010-01-07 08:55:54 +010080/***************************************************/
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 Denk54841ab2010-06-28 22:00:46 +020093int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher88ffb262010-01-07 08:55:54 +010094{
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 Schocher62a813b2011-05-03 02:15:15 +0000114 part.offset = addr - OFFSET_ADJUSTMENT;
Heiko Schocher88ffb262010-01-07 08:55:54 +0100115
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 Schocher88ffb262010-01-07 08:55:54 +0100135 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
136 size, offset);
Simon Glass41ef3722013-02-24 17:33:22 +0000137 setenv_hex("filesize", size);
Heiko Schocher88ffb262010-01-07 08:55:54 +0100138 } 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 Denk54841ab2010-06-28 22:00:46 +0200156int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher88ffb262010-01-07 08:55:54 +0100157{
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 Schocher62a813b2011-05-03 02:15:15 +0000175 part.offset = addr - OFFSET_ADJUSTMENT;
Heiko Schocher88ffb262010-01-07 08:55:54 +0100176
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/***************************************************/
190U_BOOT_CMD(
191 cramfsload, 3, 0, do_cramfs_load,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200192 "load binary file from a filesystem image",
Heiko Schocher88ffb262010-01-07 08:55:54 +0100193 "[ off ] [ filename ]\n"
194 " - load binary file from address 'cramfsaddr'\n"
195 " with offset 'off'\n"
196);
197U_BOOT_CMD(
198 cramfsls, 2, 1, do_cramfs_ls,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200199 "list files in a directory (default /)",
Heiko Schocher88ffb262010-01-07 08:55:54 +0100200 "[ directory ]\n"
201 " - list files in a directory.\n"
202);