blob: 77094c4ebbac4df3c6a66a7aed5105e25dee9789 [file] [log] [blame]
Uma Shankara1596432012-05-25 21:21:44 +05301/*
2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
6 *
7 * Ext4fs support
8 * made from existing cmd_ext2.c file of Uboot
9 *
10 * (C) Copyright 2004
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 *
14 * made from cmd_reiserfs by
15 *
16 * (C) Copyright 2003 - 2004
17 * Sysgo Real-Time Solutions, AG <www.elinos.com>
18 * Pavel Bartusek <pba@sysgo.com>
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 * MA 02111-1307 USA
34 *
35 */
36
37/*
38 * Changelog:
39 * 0.1 - Newly created file for ext4fs support. Taken from cmd_ext2.c
40 * file in uboot. Added ext4fs ls load and write support.
41 */
42
43#include <common.h>
44#include <part.h>
45#include <config.h>
46#include <command.h>
47#include <image.h>
48#include <linux/ctype.h>
49#include <asm/byteorder.h>
50#include <ext_common.h>
51#include <ext4fs.h>
52#include <linux/stat.h>
53#include <malloc.h>
54
55#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
56#include <usb.h>
57#endif
58
59#if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
60#error DOS or EFI partition support must be selected
61#endif
62
63uint64_t total_sector;
64uint64_t part_offset;
Uma Shankared34f342012-05-25 21:22:49 +053065#if defined(CONFIG_CMD_EXT4_WRITE)
66static uint64_t part_size;
67static uint16_t cur_part = 1;
68#endif
Uma Shankara1596432012-05-25 21:21:44 +053069
70#define DOS_PART_MAGIC_OFFSET 0x1fe
71#define DOS_FS_TYPE_OFFSET 0x36
72#define DOS_FS32_TYPE_OFFSET 0x52
73
74int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
75 char *const argv[])
76{
77 if (do_ext_load(cmdtp, flag, argc, argv))
78 return -1;
79
80 return 0;
81}
82
83int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
84{
85 if (do_ext_ls(cmdtp, flag, argc, argv))
86 return -1;
87
88 return 0;
89}
90
Uma Shankared34f342012-05-25 21:22:49 +053091#if defined(CONFIG_CMD_EXT4_WRITE)
92static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
93{
94 unsigned char buffer[SECTOR_SIZE];
95 disk_partition_t info;
96
97 if (!dev_desc->block_read)
98 return -1;
99
100 /* check if we have a MBR (on floppies we have only a PBR) */
101 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
102 printf("** Can't read from device %d **\n", dev_desc->dev);
103 return -1;
104 }
105 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
106 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
107 /* no signature found */
108 return -1;
109 }
110
111 /* First we assume there is a MBR */
112 if (!get_partition_info(dev_desc, part_no, &info)) {
113 part_offset = info.start;
114 cur_part = part_no;
115 part_size = info.size;
116 } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
117 "FAT", 3) == 0) || (strncmp((char *)&buffer
118 [DOS_FS32_TYPE_OFFSET],
119 "FAT32", 5) == 0)) {
120 /* ok, we assume we are on a PBR only */
121 cur_part = 1;
122 part_offset = 0;
123 } else {
124 printf("** Partition %d not valid on device %d **\n",
125 part_no, dev_desc->dev);
126 return -1;
127 }
128
129 return 0;
130}
131
132int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
133 char *const argv[])
134{
135 const char *filename = "/";
136 int part_length;
137 unsigned long part = 1;
138 int dev;
139 char *ep;
140 unsigned long ram_address;
141 unsigned long file_size;
142 disk_partition_t info;
143 struct ext_filesystem *fs;
144
145 if (argc < 6)
146 return cmd_usage(cmdtp);
147
148 dev = (int)simple_strtoul(argv[2], &ep, 16);
149 ext4_dev_desc = get_dev(argv[1], dev);
150 if (ext4_dev_desc == NULL) {
151 printf("Block device %s %d not supported\n", argv[1], dev);
152 return 1;
153 }
154 if (init_fs(ext4_dev_desc))
155 return 1;
156
157 fs = get_fs();
158 if (*ep) {
159 if (*ep != ':') {
160 puts("Invalid boot device, use `dev[:part]'\n");
161 goto fail;
162 }
163 part = simple_strtoul(++ep, NULL, 16);
164 }
165
166 /* get the filename */
167 filename = argv[3];
168
169 /* get the address in hexadecimal format (string to int) */
170 ram_address = simple_strtoul(argv[4], NULL, 16);
171
172 /* get the filesize in base 10 format */
173 file_size = simple_strtoul(argv[5], NULL, 10);
174
175 /* set the device as block device */
176 part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
177 if (part_length == 0) {
178 printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
179 goto fail;
180 }
181
182 /* register the device and partition */
183 if (ext4_register_device(fs->dev_desc, part) != 0) {
184 printf("Unable to use %s %d:%lu for fattable\n",
185 argv[1], dev, part);
186 goto fail;
187 }
188
189 /* get the partition information */
190 if (!get_partition_info(fs->dev_desc, part, &info)) {
191 total_sector = (info.size * info.blksz) / SECTOR_SIZE;
192 fs->total_sect = total_sector;
193 } else {
194 printf("error : get partition info\n");
195 goto fail;
196 }
197
198 /* mount the filesystem */
199 if (!ext4fs_mount(part_length)) {
200 printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
201 goto fail;
202 }
203
204 /* start write */
205 if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
206 printf("** Error ext4fs_write() **\n");
207 goto fail;
208 }
209 ext4fs_close();
210 deinit_fs(fs->dev_desc);
211
212 return 0;
213
214fail:
215 ext4fs_close();
216 deinit_fs(fs->dev_desc);
217
218 return 1;
219}
220
221U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
222 "create a file in the root directory",
223 "<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
224 " - create a file in / directory");
225
226#endif
227
Uma Shankara1596432012-05-25 21:21:44 +0530228U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
229 "list files in a directory (default /)",
230 "<interface> <dev[:part]> [directory]\n"
231 " - list files from 'dev' on 'interface' in a 'directory'");
232
233U_BOOT_CMD(ext4load, 6, 0, do_ext4_load,
234 "load binary file from a Ext4 filesystem",
235 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
236 " - load binary file 'filename' from 'dev' on 'interface'\n"
237 " to address 'addr' from ext4 filesystem");