cmd_zfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  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. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <common.h>
  26. #include <part.h>
  27. #include <config.h>
  28. #include <command.h>
  29. #include <image.h>
  30. #include <linux/ctype.h>
  31. #include <asm/byteorder.h>
  32. #include <zfs_common.h>
  33. #include <linux/stat.h>
  34. #include <malloc.h>
  35. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  36. #include <usb.h>
  37. #endif
  38. #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
  39. #error DOS or EFI partition support must be selected
  40. #endif
  41. #define DOS_PART_MAGIC_OFFSET 0x1fe
  42. #define DOS_FS_TYPE_OFFSET 0x36
  43. #define DOS_FS32_TYPE_OFFSET 0x52
  44. static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  45. {
  46. char *filename = NULL;
  47. int dev;
  48. int part;
  49. ulong addr = 0;
  50. disk_partition_t info;
  51. block_dev_desc_t *dev_desc;
  52. char buf[12];
  53. unsigned long count;
  54. const char *addr_str;
  55. struct zfs_file zfile;
  56. struct device_s vdev;
  57. if (argc < 3)
  58. return CMD_RET_USAGE;
  59. count = 0;
  60. addr = simple_strtoul(argv[3], NULL, 16);
  61. filename = getenv("bootfile");
  62. switch (argc) {
  63. case 3:
  64. addr_str = getenv("loadaddr");
  65. if (addr_str != NULL)
  66. addr = simple_strtoul(addr_str, NULL, 16);
  67. else
  68. addr = CONFIG_SYS_LOAD_ADDR;
  69. break;
  70. case 4:
  71. break;
  72. case 5:
  73. filename = argv[4];
  74. break;
  75. case 6:
  76. filename = argv[4];
  77. count = simple_strtoul(argv[5], NULL, 16);
  78. break;
  79. default:
  80. return cmd_usage(cmdtp);
  81. }
  82. if (!filename) {
  83. puts("** No boot file defined **\n");
  84. return 1;
  85. }
  86. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  87. if (part < 0)
  88. return 1;
  89. dev = dev_desc->dev;
  90. printf("Loading file \"%s\" from %s device %d%c%c\n",
  91. filename, argv[1], dev,
  92. part ? ':' : ' ', part ? part + '0' : ' ');
  93. zfs_set_blk_dev(dev_desc, &info);
  94. vdev.part_length = info.size;
  95. memset(&zfile, 0, sizeof(zfile));
  96. zfile.device = &vdev;
  97. if (zfs_open(&zfile, filename)) {
  98. printf("** File not found %s\n", filename);
  99. return 1;
  100. }
  101. if ((count < zfile.size) && (count != 0))
  102. zfile.size = (uint64_t)count;
  103. if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) {
  104. printf("** Unable to read \"%s\" from %s %d:%d **\n",
  105. filename, argv[1], dev, part);
  106. zfs_close(&zfile);
  107. return 1;
  108. }
  109. zfs_close(&zfile);
  110. /* Loading ok, update default load address */
  111. load_addr = addr;
  112. printf("%llu bytes read\n", zfile.size);
  113. setenv_hex("filesize", zfile.size);
  114. return 0;
  115. }
  116. int zfs_print(const char *entry, const struct zfs_dirhook_info *data)
  117. {
  118. printf("%s %s\n",
  119. data->dir ? "<DIR> " : " ",
  120. entry);
  121. return 0; /* 0 continue, 1 stop */
  122. }
  123. static int do_zfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  124. {
  125. const char *filename = "/";
  126. int part;
  127. block_dev_desc_t *dev_desc;
  128. disk_partition_t info;
  129. struct device_s vdev;
  130. if (argc < 2)
  131. return cmd_usage(cmdtp);
  132. if (argc == 4)
  133. filename = argv[3];
  134. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  135. if (part < 0)
  136. return 1;
  137. zfs_set_blk_dev(dev_desc, &info);
  138. vdev.part_length = info.size;
  139. zfs_ls(&vdev, filename,
  140. zfs_print);
  141. return 0;
  142. }
  143. U_BOOT_CMD(zfsls, 4, 1, do_zfs_ls,
  144. "list files in a directory (default /)",
  145. "<interface> <dev[:part]> [directory]\n"
  146. " - list files from 'dev' on 'interface' in a '/DATASET/@/$dir/'");
  147. U_BOOT_CMD(zfsload, 6, 0, do_zfs_load,
  148. "load binary file from a ZFS filesystem",
  149. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  150. " - load binary file '/DATASET/@/$dir/$file' from 'dev' on 'interface'\n"
  151. " to address 'addr' from ZFS filesystem");