blob: ecfc6d3c9bb077fa523ba6c476be388906862fbc [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020020 * SPDX-License-Identifier: GPL-2.0+
Uma Shankara1596432012-05-25 21:21:44 +053021 */
22
23/*
24 * Changelog:
25 * 0.1 - Newly created file for ext4fs support. Taken from cmd_ext2.c
26 * file in uboot. Added ext4fs ls load and write support.
27 */
28
29#include <common.h>
30#include <part.h>
31#include <config.h>
32#include <command.h>
33#include <image.h>
34#include <linux/ctype.h>
35#include <asm/byteorder.h>
Uma Shankara1596432012-05-25 21:21:44 +053036#include <ext4fs.h>
37#include <linux/stat.h>
38#include <malloc.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000039#include <fs.h>
Uma Shankara1596432012-05-25 21:21:44 +053040
41#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
42#include <usb.h>
43#endif
44
Stephen Warrencf659812014-06-11 12:47:26 -060045int do_ext4_size(cmd_tbl_t *cmdtp, int flag, int argc,
46 char *const argv[])
47{
48 return do_size(cmdtp, flag, argc, argv, FS_TYPE_EXT);
49}
50
Uma Shankara1596432012-05-25 21:21:44 +053051int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
52 char *const argv[])
53{
Wolfgang Denkb770e882013-10-05 21:07:25 +020054 return do_load(cmdtp, flag, argc, argv, FS_TYPE_EXT);
Uma Shankara1596432012-05-25 21:21:44 +053055}
56
57int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
58{
Stephen Warren045fa1e2012-10-22 06:43:51 +000059 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_EXT);
Uma Shankara1596432012-05-25 21:21:44 +053060}
61
Uma Shankared34f342012-05-25 21:22:49 +053062#if defined(CONFIG_CMD_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +053063int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
64 char *const argv[])
65{
66 const char *filename = "/";
Rob Herring81180812012-08-23 11:31:46 +000067 int dev, part;
Uma Shankared34f342012-05-25 21:22:49 +053068 unsigned long ram_address;
69 unsigned long file_size;
70 disk_partition_t info;
Rob Herring81180812012-08-23 11:31:46 +000071 block_dev_desc_t *dev_desc;
Uma Shankared34f342012-05-25 21:22:49 +053072
73 if (argc < 6)
74 return cmd_usage(cmdtp);
75
Stephen Warren10a37fd2012-09-21 09:50:57 +000076 part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring81180812012-08-23 11:31:46 +000077 if (part < 0)
Uma Shankared34f342012-05-25 21:22:49 +053078 return 1;
Uma Shankared34f342012-05-25 21:22:49 +053079
Rob Herring81180812012-08-23 11:31:46 +000080 dev = dev_desc->dev;
Uma Shankared34f342012-05-25 21:22:49 +053081
82 /* get the filename */
Tom Rini0171d522013-03-20 04:21:38 +000083 filename = argv[4];
Uma Shankared34f342012-05-25 21:22:49 +053084
85 /* get the address in hexadecimal format (string to int) */
Tom Rini0171d522013-03-20 04:21:38 +000086 ram_address = simple_strtoul(argv[3], NULL, 16);
Uma Shankared34f342012-05-25 21:22:49 +053087
Wolfgang Denkf7740f72014-01-31 09:28:25 +010088 /* get the filesize in hexadecimal format */
89 file_size = simple_strtoul(argv[5], NULL, 16);
Uma Shankared34f342012-05-25 21:22:49 +053090
91 /* set the device as block device */
Rob Herring81180812012-08-23 11:31:46 +000092 ext4fs_set_blk_dev(dev_desc, &info);
Uma Shankared34f342012-05-25 21:22:49 +053093
94 /* mount the filesystem */
Rob Herring81180812012-08-23 11:31:46 +000095 if (!ext4fs_mount(info.size)) {
Simon Glassb9dc4942012-10-23 13:49:25 +000096 printf("Bad ext4 partition %s %d:%d\n", argv[1], dev, part);
Uma Shankared34f342012-05-25 21:22:49 +053097 goto fail;
98 }
99
100 /* start write */
101 if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
102 printf("** Error ext4fs_write() **\n");
103 goto fail;
104 }
105 ext4fs_close();
Uma Shankared34f342012-05-25 21:22:49 +0530106
107 return 0;
108
109fail:
110 ext4fs_close();
Uma Shankared34f342012-05-25 21:22:49 +0530111
112 return 1;
113}
114
115U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
116 "create a file in the root directory",
Tom Rini0171d522013-03-20 04:21:38 +0000117 "<interface> <dev[:part]> <addr> <absolute filename path> [sizebytes]\n"
Stephen Warrenb6a30442012-10-30 12:04:18 +0000118 " - create a file in / directory");
Uma Shankared34f342012-05-25 21:22:49 +0530119
120#endif
121
Stephen Warrencf659812014-06-11 12:47:26 -0600122U_BOOT_CMD(
123 ext4size, 4, 0, do_ext4_size,
124 "determine a file's size",
125 "<interface> <dev[:part]> <filename>\n"
126 " - Find file 'filename' from 'dev' on 'interface'\n"
127 " and determine its size."
128);
129
Uma Shankara1596432012-05-25 21:21:44 +0530130U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
131 "list files in a directory (default /)",
132 "<interface> <dev[:part]> [directory]\n"
Stephen Warrenb6a30442012-10-30 12:04:18 +0000133 " - list files from 'dev' on 'interface' in a 'directory'");
Uma Shankara1596432012-05-25 21:21:44 +0530134
135U_BOOT_CMD(ext4load, 6, 0, do_ext4_load,
136 "load binary file from a Ext4 filesystem",
Pavel Machek2a72dcc2014-07-29 12:37:25 +0200137 "<interface> [<dev[:part]> [addr [filename [bytes [pos]]]]]\n"
Stephen Warrenb6a30442012-10-30 12:04:18 +0000138 " - load binary file 'filename' from 'dev' on 'interface'\n"
Wolfgang Denkb770e882013-10-05 21:07:25 +0200139 " to address 'addr' from ext4 filesystem");