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>
8 * made from existing cmd_ext2.c file of Uboot
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 * made from cmd_reiserfs by
16 * (C) Copyright 2003 - 2004
17 * Sysgo Real-Time Solutions, AG <www.elinos.com>
18 * Pavel Bartusek <pba@sysgo.com>
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.
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.
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,
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.
48 #include <linux/ctype.h>
49 #include <asm/byteorder.h>
50 #include <ext_common.h>
52 #include <linux/stat.h>
55 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
59 #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
60 #error DOS or EFI partition support must be selected
63 uint64_t total_sector;
65 #if defined(CONFIG_CMD_EXT4_WRITE)
66 static uint64_t part_size;
67 static uint16_t cur_part = 1;
70 #define DOS_PART_MAGIC_OFFSET 0x1fe
71 #define DOS_FS_TYPE_OFFSET 0x36
72 #define DOS_FS32_TYPE_OFFSET 0x52
74 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
77 if (do_ext_load(cmdtp, flag, argc, argv))
83 int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
85 if (do_ext_ls(cmdtp, flag, argc, argv))
91 #if defined(CONFIG_CMD_EXT4_WRITE)
92 static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
94 unsigned char buffer[SECTOR_SIZE];
95 disk_partition_t info;
97 if (!dev_desc->block_read)
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);
105 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
106 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
107 /* no signature found */
111 /* First we assume there is a MBR */
112 if (!get_partition_info(dev_desc, part_no, &info)) {
113 part_offset = info.start;
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],
120 /* ok, we assume we are on a PBR only */
124 printf("** Partition %d not valid on device %d **\n",
125 part_no, dev_desc->dev);
132 int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
135 const char *filename = "/";
137 unsigned long part = 1;
140 unsigned long ram_address;
141 unsigned long file_size;
142 disk_partition_t info;
143 struct ext_filesystem *fs;
146 return cmd_usage(cmdtp);
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);
158 puts("Invalid boot device, use `dev[:part]'\n");
161 part = simple_strtoul(++ep, NULL, 16);
164 /* get the filename */
167 /* get the address in hexadecimal format (string to int) */
168 ram_address = simple_strtoul(argv[4], NULL, 16);
170 /* get the filesize in base 10 format */
171 file_size = simple_strtoul(argv[5], NULL, 10);
173 /* set the device as block device */
174 part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
175 if (part_length == 0) {
176 printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
180 /* register the device and partition */
181 if (ext4_register_device(ext4_dev_desc, part) != 0) {
182 printf("Unable to use %s %d:%lu for fattable\n",
187 /* get the partition information */
188 if (!get_partition_info(ext4_dev_desc, part, &info)) {
189 total_sector = (info.size * info.blksz) / SECTOR_SIZE;
190 fs->total_sect = total_sector;
192 printf("error : get partition info\n");
196 /* mount the filesystem */
197 if (!ext4fs_mount(part_length)) {
198 printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
203 if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
204 printf("** Error ext4fs_write() **\n");
217 U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
218 "create a file in the root directory",
219 "<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
220 " - create a file in / directory");
224 U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
225 "list files in a directory (default /)",
226 "<interface> <dev[:part]> [directory]\n"
227 " - list files from 'dev' on 'interface' in a 'directory'");
229 U_BOOT_CMD(ext4load, 6, 0, do_ext4_load,
230 "load binary file from a Ext4 filesystem",
231 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
232 " - load binary file 'filename' from 'dev' on 'interface'\n"
233 " to address 'addr' from ext4 filesystem");