Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * ZFS filesystem porting to Uboot by |
| 5 | * Jorgen Lundman <lundman at lundman.net> |
| 6 | * |
| 7 | * zfsfs support |
| 8 | * made from existing GRUB Sources by Sun, GNU and others. |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 11 | #include <part.h> |
| 12 | #include <config.h> |
| 13 | #include <command.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 14 | #include <env.h> |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 15 | #include <image.h> |
| 16 | #include <linux/ctype.h> |
| 17 | #include <asm/byteorder.h> |
| 18 | #include <zfs_common.h> |
| 19 | #include <linux/stat.h> |
| 20 | #include <malloc.h> |
| 21 | |
| 22 | #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) |
| 23 | #include <usb.h> |
| 24 | #endif |
| 25 | |
Patrick Delaunay | 8a4f2bd | 2017-01-27 11:00:41 +0100 | [diff] [blame] | 26 | #if !CONFIG_IS_ENABLED(DOS_PARTITION) && !CONFIG_IS_ENABLED(EFI_PARTITION) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 27 | #error DOS or EFI partition support must be selected |
| 28 | #endif |
| 29 | |
| 30 | #define DOS_PART_MAGIC_OFFSET 0x1fe |
| 31 | #define DOS_FS_TYPE_OFFSET 0x36 |
| 32 | #define DOS_FS32_TYPE_OFFSET 0x52 |
| 33 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 34 | static int do_zfs_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 35 | char *const argv[]) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 36 | { |
| 37 | char *filename = NULL; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 38 | int dev; |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 39 | int part; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 40 | ulong addr = 0; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 41 | struct disk_partition info; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 42 | struct blk_desc *dev_desc; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 43 | unsigned long count; |
| 44 | const char *addr_str; |
| 45 | struct zfs_file zfile; |
| 46 | struct device_s vdev; |
| 47 | |
| 48 | if (argc < 3) |
| 49 | return CMD_RET_USAGE; |
| 50 | |
| 51 | count = 0; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 52 | addr = hextoul(argv[3], NULL); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 53 | filename = env_get("bootfile"); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 54 | switch (argc) { |
| 55 | case 3: |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 56 | addr_str = env_get("loadaddr"); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 57 | if (addr_str != NULL) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 58 | addr = hextoul(addr_str, NULL); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 59 | else |
| 60 | addr = CONFIG_SYS_LOAD_ADDR; |
| 61 | |
| 62 | break; |
| 63 | case 4: |
| 64 | break; |
| 65 | case 5: |
| 66 | filename = argv[4]; |
| 67 | break; |
| 68 | case 6: |
| 69 | filename = argv[4]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 70 | count = hextoul(argv[5], NULL); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | default: |
| 74 | return cmd_usage(cmdtp); |
| 75 | } |
| 76 | |
| 77 | if (!filename) { |
| 78 | puts("** No boot file defined **\n"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 82 | part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1); |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 83 | if (part < 0) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 84 | return 1; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 85 | |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 86 | dev = dev_desc->devnum; |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 87 | printf("Loading file \"%s\" from %s device %d%c%c\n", |
| 88 | filename, argv[1], dev, |
| 89 | part ? ':' : ' ', part ? part + '0' : ' '); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 90 | |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 91 | zfs_set_blk_dev(dev_desc, &info); |
| 92 | vdev.part_length = info.size; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 93 | |
| 94 | memset(&zfile, 0, sizeof(zfile)); |
| 95 | zfile.device = &vdev; |
| 96 | if (zfs_open(&zfile, filename)) { |
Luka Perkov | e077389 | 2013-10-10 01:32:28 +0200 | [diff] [blame] | 97 | printf("** File not found %s **\n", filename); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | if ((count < zfile.size) && (count != 0)) |
| 102 | zfile.size = (uint64_t)count; |
| 103 | |
| 104 | if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) { |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 105 | printf("** Unable to read \"%s\" from %s %d:%d **\n", |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 106 | filename, argv[1], dev, part); |
| 107 | zfs_close(&zfile); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | zfs_close(&zfile); |
| 112 | |
| 113 | /* Loading ok, update default load address */ |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 114 | image_load_addr = addr; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 115 | |
| 116 | printf("%llu bytes read\n", zfile.size); |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 117 | env_set_hex("filesize", zfile.size); |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 122 | int zfs_print(const char *entry, const struct zfs_dirhook_info *data) |
| 123 | { |
| 124 | printf("%s %s\n", |
| 125 | data->dir ? "<DIR> " : " ", |
| 126 | entry); |
| 127 | return 0; /* 0 continue, 1 stop */ |
| 128 | } |
| 129 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 130 | static int do_zfs_ls(struct cmd_tbl *cmdtp, int flag, int argc, |
| 131 | char *const argv[]) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 132 | { |
| 133 | const char *filename = "/"; |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 134 | int part; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 135 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 136 | struct disk_partition info; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 137 | struct device_s vdev; |
| 138 | |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 139 | if (argc < 2) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 140 | return cmd_usage(cmdtp); |
| 141 | |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 142 | if (argc == 4) |
| 143 | filename = argv[3]; |
| 144 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 145 | part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1); |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 146 | if (part < 0) |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 147 | return 1; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 148 | |
Rob Herring | 153817e | 2012-08-23 11:31:49 +0000 | [diff] [blame] | 149 | zfs_set_blk_dev(dev_desc, &info); |
| 150 | vdev.part_length = info.size; |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 151 | |
| 152 | zfs_ls(&vdev, filename, |
| 153 | zfs_print); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Jorgen Lundman | 9b4a1f9 | 2012-07-19 20:48:25 +0000 | [diff] [blame] | 158 | U_BOOT_CMD(zfsls, 4, 1, do_zfs_ls, |
| 159 | "list files in a directory (default /)", |
| 160 | "<interface> <dev[:part]> [directory]\n" |
| 161 | " - list files from 'dev' on 'interface' in a '/DATASET/@/$dir/'"); |
| 162 | |
| 163 | U_BOOT_CMD(zfsload, 6, 0, do_zfs_load, |
| 164 | "load binary file from a ZFS filesystem", |
| 165 | "<interface> <dev[:part]> [addr] [filename] [bytes]\n" |
| 166 | " - load binary file '/DATASET/@/$dir/$file' from 'dev' on 'interface'\n" |
| 167 | " to address 'addr' from ZFS filesystem"); |