cmd_ext4.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT4 filesystem implementation in Uboot by
  4. * Uma Shankar <uma.shankar@samsung.com>
  5. * Manjunatha C Achar <a.manjunatha@samsung.com>
  6. *
  7. * Ext4fs support
  8. * made from existing cmd_ext2.c file of Uboot
  9. *
  10. * (C) Copyright 2004
  11. * esd gmbh <www.esd-electronics.com>
  12. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  13. *
  14. * made from cmd_reiserfs by
  15. *
  16. * (C) Copyright 2003 - 2004
  17. * Sysgo Real-Time Solutions, AG <www.elinos.com>
  18. * Pavel Bartusek <pba@sysgo.com>
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of
  23. * the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  33. * MA 02111-1307 USA
  34. *
  35. */
  36. /*
  37. * Changelog:
  38. * 0.1 - Newly created file for ext4fs support. Taken from cmd_ext2.c
  39. * file in uboot. Added ext4fs ls load and write support.
  40. */
  41. #include <common.h>
  42. #include <part.h>
  43. #include <config.h>
  44. #include <command.h>
  45. #include <image.h>
  46. #include <linux/ctype.h>
  47. #include <asm/byteorder.h>
  48. #include <ext_common.h>
  49. #include <ext4fs.h>
  50. #include <linux/stat.h>
  51. #include <malloc.h>
  52. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  53. #include <usb.h>
  54. #endif
  55. #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
  56. #error DOS or EFI partition support must be selected
  57. #endif
  58. uint64_t total_sector;
  59. uint64_t part_offset;
  60. #if defined(CONFIG_CMD_EXT4_WRITE)
  61. static uint64_t part_size;
  62. static uint16_t cur_part = 1;
  63. #endif
  64. #define DOS_PART_MAGIC_OFFSET 0x1fe
  65. #define DOS_FS_TYPE_OFFSET 0x36
  66. #define DOS_FS32_TYPE_OFFSET 0x52
  67. int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
  68. char *const argv[])
  69. {
  70. if (do_ext_load(cmdtp, flag, argc, argv))
  71. return -1;
  72. return 0;
  73. }
  74. int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  75. {
  76. if (do_ext_ls(cmdtp, flag, argc, argv))
  77. return -1;
  78. return 0;
  79. }
  80. #if defined(CONFIG_CMD_EXT4_WRITE)
  81. static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
  82. {
  83. unsigned char buffer[SECTOR_SIZE];
  84. disk_partition_t info;
  85. if (!dev_desc->block_read)
  86. return -1;
  87. /* check if we have a MBR (on floppies we have only a PBR) */
  88. if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
  89. printf("** Can't read from device %d **\n", dev_desc->dev);
  90. return -1;
  91. }
  92. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  93. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  94. /* no signature found */
  95. return -1;
  96. }
  97. /* First we assume there is a MBR */
  98. if (!get_partition_info(dev_desc, part_no, &info)) {
  99. part_offset = info.start;
  100. cur_part = part_no;
  101. part_size = info.size;
  102. } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
  103. "FAT", 3) == 0) || (strncmp((char *)&buffer
  104. [DOS_FS32_TYPE_OFFSET],
  105. "FAT32", 5) == 0)) {
  106. /* ok, we assume we are on a PBR only */
  107. cur_part = 1;
  108. part_offset = 0;
  109. } else {
  110. printf("** Partition %d not valid on device %d **\n",
  111. part_no, dev_desc->dev);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
  117. char *const argv[])
  118. {
  119. const char *filename = "/";
  120. int part_length;
  121. unsigned long part = 1;
  122. int dev;
  123. char *ep;
  124. unsigned long ram_address;
  125. unsigned long file_size;
  126. disk_partition_t info;
  127. struct ext_filesystem *fs;
  128. if (argc < 6)
  129. return cmd_usage(cmdtp);
  130. dev = (int)simple_strtoul(argv[2], &ep, 16);
  131. ext4_dev_desc = get_dev(argv[1], dev);
  132. if (ext4_dev_desc == NULL) {
  133. printf("Block device %s %d not supported\n", argv[1], dev);
  134. return 1;
  135. }
  136. if (init_fs(ext4_dev_desc))
  137. return 1;
  138. fs = get_fs();
  139. if (*ep) {
  140. if (*ep != ':') {
  141. puts("Invalid boot device, use `dev[:part]'\n");
  142. goto fail;
  143. }
  144. part = simple_strtoul(++ep, NULL, 16);
  145. }
  146. /* get the filename */
  147. filename = argv[3];
  148. /* get the address in hexadecimal format (string to int) */
  149. ram_address = simple_strtoul(argv[4], NULL, 16);
  150. /* get the filesize in base 10 format */
  151. file_size = simple_strtoul(argv[5], NULL, 10);
  152. /* set the device as block device */
  153. part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
  154. if (part_length == 0) {
  155. printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
  156. goto fail;
  157. }
  158. /* register the device and partition */
  159. if (ext4_register_device(fs->dev_desc, part) != 0) {
  160. printf("Unable to use %s %d:%lu for fattable\n",
  161. argv[1], dev, part);
  162. goto fail;
  163. }
  164. /* get the partition information */
  165. if (!get_partition_info(fs->dev_desc, part, &info)) {
  166. total_sector = (info.size * info.blksz) / SECTOR_SIZE;
  167. fs->total_sect = total_sector;
  168. } else {
  169. printf("error : get partition info\n");
  170. goto fail;
  171. }
  172. /* mount the filesystem */
  173. if (!ext4fs_mount(part_length)) {
  174. printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
  175. goto fail;
  176. }
  177. /* start write */
  178. if (ext4fs_write(filename, (unsigned char *)ram_address, file_size)) {
  179. printf("** Error ext4fs_write() **\n");
  180. goto fail;
  181. }
  182. ext4fs_close();
  183. deinit_fs(fs->dev_desc);
  184. return 0;
  185. fail:
  186. ext4fs_close();
  187. deinit_fs(fs->dev_desc);
  188. return 1;
  189. }
  190. U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
  191. "create a file in the root directory",
  192. "<interface> <dev[:part]> [Absolute filename path] [Address] [sizebytes]\n"
  193. " - create a file in / directory");
  194. #endif
  195. U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
  196. "list files in a directory (default /)",
  197. "<interface> <dev[:part]> [directory]\n"
  198. " - list files from 'dev' on 'interface' in a 'directory'");
  199. U_BOOT_CMD(ext4load, 6, 0, do_ext4_load,
  200. "load binary file from a Ext4 filesystem",
  201. "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
  202. " - load binary file 'filename' from 'dev' on 'interface'\n"
  203. " to address 'addr' from ext4 filesystem");