blob: b438ce16c91136d1c645c5546064b799eab599ee [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2002
4 * Richard Jones, rjones@nexus-tech.net
wdenk7a428cc2003-06-15 22:40:42 +00005 */
6
7/*
8 * Boot support
9 */
10#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050012#include <mapmem.h>
wdenk7a428cc2003-06-15 22:40:42 +000013#include <fat.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000014#include <fs.h>
Simon Glass655306c2020-05-10 11:39:58 -060015#include <part.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
wdenk2c9b05d2003-09-10 22:30:53 +000017
Simon Glassed38aef2020-05-10 11:40:03 -060018int do_fat_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stephen Warren3eb58f52014-06-11 12:47:26 -060019{
20 return do_size(cmdtp, flag, argc, argv, FS_TYPE_FAT);
21}
22
23U_BOOT_CMD(
24 fatsize, 4, 0, do_fat_size,
25 "determine a file's size",
26 "<interface> <dev[:part]> <filename>\n"
27 " - Find file 'filename' from 'dev' on 'interface'\n"
28 " and determine its size."
29);
30
Simon Glassed38aef2020-05-10 11:40:03 -060031int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk7a428cc2003-06-15 22:40:42 +000032{
Wolfgang Denka10c7a52013-10-05 21:07:25 +020033 return do_load(cmdtp, flag, argc, argv, FS_TYPE_FAT);
wdenk7a428cc2003-06-15 22:40:42 +000034}
35
wdenk2c9b05d2003-09-10 22:30:53 +000036
wdenkf287a242003-07-01 21:06:45 +000037U_BOOT_CMD(
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +000038 fatload, 7, 0, do_fat_fsload,
Peter Tyserdfb72b82009-01-27 18:03:12 -060039 "load binary file from a dos filesystem",
Pavel Machek264ebb52014-07-09 22:40:07 +020040 "<interface> [<dev[:part]> [<addr> [<filename> [bytes [pos]]]]]\n"
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +000041 " - Load binary file 'filename' from 'dev' on 'interface'\n"
42 " to address 'addr' from dos filesystem.\n"
43 " 'pos' gives the file position to start loading from.\n"
44 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
45 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
Stephen Warren40816a02012-10-30 12:04:19 +000046 " the load stops on end of file.\n"
Tom Rini9890c682013-02-21 06:55:40 +000047 " If either 'pos' or 'bytes' are not aligned to\n"
48 " ARCH_DMA_MINALIGN then a misaligned buffer warning will\n"
Wolfgang Denka10c7a52013-10-05 21:07:25 +020049 " be printed and performance will suffer for the load."
wdenkf12e3962003-06-29 21:03:46 +000050);
51
Simon Glassed38aef2020-05-10 11:40:03 -060052static int do_fat_ls(struct cmd_tbl *cmdtp, int flag, int argc,
53 char *const argv[])
wdenk7a428cc2003-06-15 22:40:42 +000054{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000055 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_FAT);
wdenk7a428cc2003-06-15 22:40:42 +000056}
57
wdenkf287a242003-07-01 21:06:45 +000058U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +000059 fatls, 4, 1, do_fat_ls,
Peter Tyserdfb72b82009-01-27 18:03:12 -060060 "list files in a directory (default /)",
Rob Herring5fc26df2012-08-23 11:31:47 +000061 "<interface> [<dev[:part]>] [directory]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +020062 " - list files from 'dev' on 'interface' in a 'directory'"
wdenkf12e3962003-06-29 21:03:46 +000063);
64
Simon Glassed38aef2020-05-10 11:40:03 -060065static int do_fat_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
66 char *const argv[])
wdenk7a428cc2003-06-15 22:40:42 +000067{
Rob Herring5fc26df2012-08-23 11:31:47 +000068 int dev, part;
Simon Glasse3394752016-02-29 15:25:34 -070069 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060070 struct disk_partition info;
wdenk7a428cc2003-06-15 22:40:42 +000071
wdenk2c9b05d2003-09-10 22:30:53 +000072 if (argc < 2) {
Rob Herring5fc26df2012-08-23 11:31:47 +000073 printf("usage: fatinfo <interface> [<dev[:part]>]\n");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020074 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +000075 }
Rob Herring5fc26df2012-08-23 11:31:47 +000076
Simon Glasse76ee972016-02-29 15:25:44 -070077 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring5fc26df2012-08-23 11:31:47 +000078 if (part < 0)
wdenk2c9b05d2003-09-10 22:30:53 +000079 return 1;
Rob Herring5fc26df2012-08-23 11:31:47 +000080
Simon Glass2f26fff2016-02-29 15:25:51 -070081 dev = dev_desc->devnum;
Stephen Warren288904d2012-10-17 06:44:59 +000082 if (fat_set_blk_dev(dev_desc, &info) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020083 printf("\n** Unable to use %s %d:%d for fatinfo **\n",
84 argv[1], dev, part);
wdenk2c9b05d2003-09-10 22:30:53 +000085 return 1;
86 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020087 return file_fat_detectfs();
wdenk7a428cc2003-06-15 22:40:42 +000088}
89
wdenkf287a242003-07-01 21:06:45 +000090U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +000091 fatinfo, 3, 1, do_fat_fsinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -060092 "print information about filesystem",
Rob Herring5fc26df2012-08-23 11:31:47 +000093 "<interface> [<dev[:part]>]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +020094 " - print information about filesystem from 'dev' on 'interface'"
wdenkf12e3962003-06-29 21:03:46 +000095);
Donggeun Kimb44c8ab2012-03-22 04:38:56 +000096
97#ifdef CONFIG_FAT_WRITE
Simon Glassed38aef2020-05-10 11:40:03 -060098static int do_fat_fswrite(struct cmd_tbl *cmdtp, int flag, int argc,
99 char *const argv[])
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000100{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800101 loff_t size;
102 int ret;
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000103 unsigned long addr;
104 unsigned long count;
AKASHI Takahirof63931e2018-09-11 15:59:07 +0900105 long offset;
Simon Glasse3394752016-02-29 15:25:34 -0700106 struct blk_desc *dev_desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600107 struct disk_partition info;
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000108 int dev = 0;
109 int part = 1;
Wu, Josh77474032014-06-04 11:01:24 +0800110 void *buf;
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000111
112 if (argc < 5)
113 return cmd_usage(cmdtp);
114
Simon Glasse76ee972016-02-29 15:25:44 -0700115 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring5fc26df2012-08-23 11:31:47 +0000116 if (part < 0)
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000117 return 1;
Rob Herring5fc26df2012-08-23 11:31:47 +0000118
Simon Glass2f26fff2016-02-29 15:25:51 -0700119 dev = dev_desc->devnum;
Rob Herring5fc26df2012-08-23 11:31:47 +0000120
Stephen Warren288904d2012-10-17 06:44:59 +0000121 if (fat_set_blk_dev(dev_desc, &info) != 0) {
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000122 printf("\n** Unable to use %s %d:%d for fatwrite **\n",
123 argv[1], dev, part);
124 return 1;
125 }
126 addr = simple_strtoul(argv[3], NULL, 16);
Stefan Brüns95dbded2016-09-11 22:51:42 +0200127 count = (argc <= 5) ? 0 : simple_strtoul(argv[5], NULL, 16);
AKASHI Takahirof63931e2018-09-11 15:59:07 +0900128 /* offset should be a hex, but "-1" is allowed */
129 offset = (argc <= 6) ? 0 : simple_strtol(argv[6], NULL, 16);
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000130
Wu, Josh77474032014-06-04 11:01:24 +0800131 buf = map_sysmem(addr, count);
AKASHI Takahirof63931e2018-09-11 15:59:07 +0900132 ret = file_fat_write(argv[4], buf, offset, count, &size);
Wu, Josh77474032014-06-04 11:01:24 +0800133 unmap_sysmem(buf);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800134 if (ret < 0) {
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000135 printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
136 argv[4], argv[1], dev, part);
137 return 1;
138 }
139
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800140 printf("%llu bytes written\n", size);
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000141
142 return 0;
143}
144
145U_BOOT_CMD(
AKASHI Takahirof63931e2018-09-11 15:59:07 +0900146 fatwrite, 7, 0, do_fat_fswrite,
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000147 "write file into a dos filesystem",
AKASHI Takahirof63931e2018-09-11 15:59:07 +0900148 "<interface> <dev[:part]> <addr> <filename> [<bytes> [<offset>]]\n"
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000149 " - write file 'filename' from the address 'addr' in RAM\n"
150 " to 'dev' on 'interface'"
151);
AKASHI Takahiroe31874e2018-09-11 15:59:11 +0900152
Simon Glassed38aef2020-05-10 11:40:03 -0600153static int do_fat_rm(struct cmd_tbl *cmdtp, int flag, int argc,
154 char *const argv[])
AKASHI Takahiro944eab82018-09-11 15:59:15 +0900155{
156 return do_rm(cmdtp, flag, argc, argv, FS_TYPE_FAT);
157}
158
159U_BOOT_CMD(
160 fatrm, 4, 1, do_fat_rm,
161 "delete a file",
162 "<interface> [<dev[:part]>] <filename>\n"
163 " - delete a file from 'dev' on 'interface'"
164);
165
Simon Glassed38aef2020-05-10 11:40:03 -0600166static int do_fat_mkdir(struct cmd_tbl *cmdtp, int flag, int argc,
167 char *const argv[])
AKASHI Takahiroe31874e2018-09-11 15:59:11 +0900168{
169 return do_mkdir(cmdtp, flag, argc, argv, FS_TYPE_FAT);
170}
171
172U_BOOT_CMD(
173 fatmkdir, 4, 1, do_fat_mkdir,
174 "create a directory",
175 "<interface> [<dev[:part]>] <directory>\n"
176 " - create a directory in 'dev' on 'interface'"
177);
Donggeun Kimb44c8ab2012-03-22 04:38:56 +0000178#endif