blob: 0aed29e9b2c8989d86c3ff14fad67feca692b1f8 [file] [log] [blame]
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001/*
2 *
3 * ZFS filesystem porting to Uboot by
4 * Jorgen Lundman <lundman at lundman.net>
5 *
6 * zfsfs support
7 * made from existing GRUB Sources by Sun, GNU and others.
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000010 */
11
12#include <common.h>
13#include <part.h>
14#include <config.h>
15#include <command.h>
16#include <image.h>
17#include <linux/ctype.h>
18#include <asm/byteorder.h>
19#include <zfs_common.h>
20#include <linux/stat.h>
21#include <malloc.h>
22
23#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
24#include <usb.h>
25#endif
26
27#if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
28#error DOS or EFI partition support must be selected
29#endif
30
31#define DOS_PART_MAGIC_OFFSET 0x1fe
32#define DOS_FS_TYPE_OFFSET 0x36
33#define DOS_FS32_TYPE_OFFSET 0x52
34
Stefan Roeseaf8d1d32012-10-26 04:24:00 +000035static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000036{
37 char *filename = NULL;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000038 int dev;
Rob Herring41204572012-08-23 11:31:49 +000039 int part;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000040 ulong addr = 0;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000041 disk_partition_t info;
Rob Herring41204572012-08-23 11:31:49 +000042 block_dev_desc_t *dev_desc;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000043 char buf[12];
44 unsigned long count;
45 const char *addr_str;
46 struct zfs_file zfile;
47 struct device_s vdev;
48
49 if (argc < 3)
50 return CMD_RET_USAGE;
51
52 count = 0;
53 addr = simple_strtoul(argv[3], NULL, 16);
54 filename = getenv("bootfile");
55 switch (argc) {
56 case 3:
57 addr_str = getenv("loadaddr");
58 if (addr_str != NULL)
59 addr = simple_strtoul(addr_str, NULL, 16);
60 else
61 addr = CONFIG_SYS_LOAD_ADDR;
62
63 break;
64 case 4:
65 break;
66 case 5:
67 filename = argv[4];
68 break;
69 case 6:
70 filename = argv[4];
71 count = simple_strtoul(argv[5], NULL, 16);
72 break;
73
74 default:
75 return cmd_usage(cmdtp);
76 }
77
78 if (!filename) {
79 puts("** No boot file defined **\n");
80 return 1;
81 }
82
Stephen Warren10a37fd2012-09-21 09:50:57 +000083 part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring41204572012-08-23 11:31:49 +000084 if (part < 0)
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000085 return 1;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000086
Rob Herring41204572012-08-23 11:31:49 +000087 dev = dev_desc->dev;
88 printf("Loading file \"%s\" from %s device %d%c%c\n",
89 filename, argv[1], dev,
90 part ? ':' : ' ', part ? part + '0' : ' ');
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000091
Rob Herring41204572012-08-23 11:31:49 +000092 zfs_set_blk_dev(dev_desc, &info);
93 vdev.part_length = info.size;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000094
95 memset(&zfile, 0, sizeof(zfile));
96 zfile.device = &vdev;
97 if (zfs_open(&zfile, filename)) {
Luka Perkov01adbce2013-10-10 01:32:28 +020098 printf("** File not found %s **\n", filename);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000099 return 1;
100 }
101
102 if ((count < zfile.size) && (count != 0))
103 zfile.size = (uint64_t)count;
104
105 if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) {
Rob Herring41204572012-08-23 11:31:49 +0000106 printf("** Unable to read \"%s\" from %s %d:%d **\n",
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000107 filename, argv[1], dev, part);
108 zfs_close(&zfile);
109 return 1;
110 }
111
112 zfs_close(&zfile);
113
114 /* Loading ok, update default load address */
115 load_addr = addr;
116
117 printf("%llu bytes read\n", zfile.size);
Simon Glass41ef3722013-02-24 17:33:22 +0000118 setenv_hex("filesize", zfile.size);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000119
120 return 0;
121}
122
123
124int zfs_print(const char *entry, const struct zfs_dirhook_info *data)
125{
126 printf("%s %s\n",
127 data->dir ? "<DIR> " : " ",
128 entry);
129 return 0; /* 0 continue, 1 stop */
130}
131
132
133
Stefan Roeseaf8d1d32012-10-26 04:24:00 +0000134static int do_zfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000135{
136 const char *filename = "/";
Rob Herring41204572012-08-23 11:31:49 +0000137 int part;
138 block_dev_desc_t *dev_desc;
139 disk_partition_t info;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000140 struct device_s vdev;
141
Rob Herring41204572012-08-23 11:31:49 +0000142 if (argc < 2)
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000143 return cmd_usage(cmdtp);
144
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000145 if (argc == 4)
146 filename = argv[3];
147
Stephen Warren10a37fd2012-09-21 09:50:57 +0000148 part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring41204572012-08-23 11:31:49 +0000149 if (part < 0)
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000150 return 1;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000151
Rob Herring41204572012-08-23 11:31:49 +0000152 zfs_set_blk_dev(dev_desc, &info);
153 vdev.part_length = info.size;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000154
155 zfs_ls(&vdev, filename,
156 zfs_print);
157
158 return 0;
159}
160
161
162U_BOOT_CMD(zfsls, 4, 1, do_zfs_ls,
163 "list files in a directory (default /)",
164 "<interface> <dev[:part]> [directory]\n"
165 " - list files from 'dev' on 'interface' in a '/DATASET/@/$dir/'");
166
167U_BOOT_CMD(zfsload, 6, 0, do_zfs_load,
168 "load binary file from a ZFS filesystem",
169 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
170 " - load binary file '/DATASET/@/$dir/$file' from 'dev' on 'interface'\n"
171 " to address 'addr' from ZFS filesystem");