xref: /openbmc/u-boot/cmd/fat.c (revision 2290fe06)
1 /*
2  * (C) Copyright 2002
3  * Richard Jones, rjones@nexus-tech.net
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * Boot support
10  */
11 #include <common.h>
12 #include <command.h>
13 #include <s_record.h>
14 #include <net.h>
15 #include <ata.h>
16 #include <asm/io.h>
17 #include <mapmem.h>
18 #include <part.h>
19 #include <fat.h>
20 #include <fs.h>
21 
22 int do_fat_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23 {
24 	return do_size(cmdtp, flag, argc, argv, FS_TYPE_FAT);
25 }
26 
27 U_BOOT_CMD(
28 	fatsize,	4,	0,	do_fat_size,
29 	"determine a file's size",
30 	"<interface> <dev[:part]> <filename>\n"
31 	"    - Find file 'filename' from 'dev' on 'interface'\n"
32 	"      and determine its size."
33 );
34 
35 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36 {
37 	return do_load(cmdtp, flag, argc, argv, FS_TYPE_FAT);
38 }
39 
40 
41 U_BOOT_CMD(
42 	fatload,	7,	0,	do_fat_fsload,
43 	"load binary file from a dos filesystem",
44 	"<interface> [<dev[:part]> [<addr> [<filename> [bytes [pos]]]]]\n"
45 	"    - Load binary file 'filename' from 'dev' on 'interface'\n"
46 	"      to address 'addr' from dos filesystem.\n"
47 	"      'pos' gives the file position to start loading from.\n"
48 	"      If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
49 	"      'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
50 	"      the load stops on end of file.\n"
51 	"      If either 'pos' or 'bytes' are not aligned to\n"
52 	"      ARCH_DMA_MINALIGN then a misaligned buffer warning will\n"
53 	"      be printed and performance will suffer for the load."
54 );
55 
56 static int do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
57 {
58 	return do_ls(cmdtp, flag, argc, argv, FS_TYPE_FAT);
59 }
60 
61 U_BOOT_CMD(
62 	fatls,	4,	1,	do_fat_ls,
63 	"list files in a directory (default /)",
64 	"<interface> [<dev[:part]>] [directory]\n"
65 	"    - list files from 'dev' on 'interface' in a 'directory'"
66 );
67 
68 static int do_fat_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc,
69 			 char * const argv[])
70 {
71 	int dev, part;
72 	struct blk_desc *dev_desc;
73 	disk_partition_t info;
74 
75 	if (argc < 2) {
76 		printf("usage: fatinfo <interface> [<dev[:part]>]\n");
77 		return 0;
78 	}
79 
80 	part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
81 	if (part < 0)
82 		return 1;
83 
84 	dev = dev_desc->devnum;
85 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
86 		printf("\n** Unable to use %s %d:%d for fatinfo **\n",
87 			argv[1], dev, part);
88 		return 1;
89 	}
90 	return file_fat_detectfs();
91 }
92 
93 U_BOOT_CMD(
94 	fatinfo,	3,	1,	do_fat_fsinfo,
95 	"print information about filesystem",
96 	"<interface> [<dev[:part]>]\n"
97 	"    - print information about filesystem from 'dev' on 'interface'"
98 );
99 
100 #ifdef CONFIG_FAT_WRITE
101 static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
102 		int argc, char * const argv[])
103 {
104 	loff_t size;
105 	int ret;
106 	unsigned long addr;
107 	unsigned long count;
108 	struct blk_desc *dev_desc = NULL;
109 	disk_partition_t info;
110 	int dev = 0;
111 	int part = 1;
112 	void *buf;
113 
114 	if (argc < 5)
115 		return cmd_usage(cmdtp);
116 
117 	part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
118 	if (part < 0)
119 		return 1;
120 
121 	dev = dev_desc->devnum;
122 
123 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
124 		printf("\n** Unable to use %s %d:%d for fatwrite **\n",
125 			argv[1], dev, part);
126 		return 1;
127 	}
128 	addr = simple_strtoul(argv[3], NULL, 16);
129 	count = simple_strtoul(argv[5], NULL, 16);
130 
131 	buf = map_sysmem(addr, count);
132 	ret = file_fat_write(argv[4], buf, 0, count, &size);
133 	unmap_sysmem(buf);
134 	if (ret < 0) {
135 		printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
136 			argv[4], argv[1], dev, part);
137 		return 1;
138 	}
139 
140 	printf("%llu bytes written\n", size);
141 
142 	return 0;
143 }
144 
145 U_BOOT_CMD(
146 	fatwrite,	6,	0,	do_fat_fswrite,
147 	"write file into a dos filesystem",
148 	"<interface> <dev[:part]> <addr> <filename> <bytes>\n"
149 	"    - write file 'filename' from the address 'addr' in RAM\n"
150 	"      to 'dev' on 'interface'"
151 );
152 #endif
153