cmd_ext_common.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT2/4 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. #define DOS_PART_MAGIC_OFFSET 0x1fe
  59. #define DOS_FS_TYPE_OFFSET 0x36
  60. #define DOS_FS32_TYPE_OFFSET 0x52
  61. int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
  62. char *const argv[])
  63. {
  64. char *filename = NULL;
  65. int dev, part;
  66. ulong addr = 0;
  67. int filelen;
  68. disk_partition_t info;
  69. block_dev_desc_t *dev_desc;
  70. char buf[12];
  71. unsigned long count;
  72. const char *addr_str;
  73. count = 0;
  74. addr = simple_strtoul(argv[3], NULL, 16);
  75. filename = getenv("bootfile");
  76. switch (argc) {
  77. case 3:
  78. addr_str = getenv("loadaddr");
  79. if (addr_str != NULL)
  80. addr = simple_strtoul(addr_str, NULL, 16);
  81. else
  82. addr = CONFIG_SYS_LOAD_ADDR;
  83. break;
  84. case 4:
  85. break;
  86. case 5:
  87. filename = argv[4];
  88. break;
  89. case 6:
  90. filename = argv[4];
  91. count = simple_strtoul(argv[5], NULL, 16);
  92. break;
  93. default:
  94. return cmd_usage(cmdtp);
  95. }
  96. if (!filename) {
  97. puts("** No boot file defined **\n");
  98. return 1;
  99. }
  100. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  101. if (part < 0)
  102. return 1;
  103. dev = dev_desc->dev;
  104. printf("Loading file \"%s\" from %s device %d%c%c\n",
  105. filename, argv[1], dev,
  106. part ? ':' : ' ', part ? part + '0' : ' ');
  107. ext4fs_set_blk_dev(dev_desc, &info);
  108. if (!ext4fs_mount(info.size)) {
  109. printf("** Bad ext2 partition or disk - %s %d:%d **\n",
  110. argv[1], dev, part);
  111. ext4fs_close();
  112. goto fail;
  113. }
  114. filelen = ext4fs_open(filename);
  115. if (filelen < 0) {
  116. printf("** File not found %s\n", filename);
  117. ext4fs_close();
  118. goto fail;
  119. }
  120. if ((count < filelen) && (count != 0))
  121. filelen = count;
  122. if (ext4fs_read((char *)addr, filelen) != filelen) {
  123. printf("** Unable to read \"%s\" from %s %d:%d **\n",
  124. filename, argv[1], dev, part);
  125. ext4fs_close();
  126. goto fail;
  127. }
  128. ext4fs_close();
  129. /* Loading ok, update default load address */
  130. load_addr = addr;
  131. printf("%d bytes read\n", filelen);
  132. sprintf(buf, "%X", filelen);
  133. setenv("filesize", buf);
  134. return 0;
  135. fail:
  136. return 1;
  137. }
  138. int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  139. {
  140. const char *filename = "/";
  141. int dev;
  142. int part;
  143. block_dev_desc_t *dev_desc;
  144. disk_partition_t info;
  145. if (argc < 2)
  146. return cmd_usage(cmdtp);
  147. part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
  148. if (part < 0)
  149. return 1;
  150. if (argc == 4)
  151. filename = argv[3];
  152. dev = dev_desc->dev;
  153. ext4fs_set_blk_dev(dev_desc, &info);
  154. if (!ext4fs_mount(info.size)) {
  155. printf("** Bad ext2 partition or disk - %s %d:%d **\n",
  156. argv[1], dev, part);
  157. ext4fs_close();
  158. goto fail;
  159. }
  160. if (ext4fs_ls(filename)) {
  161. printf("** Error extfs_ls() **\n");
  162. ext4fs_close();
  163. goto fail;
  164. };
  165. ext4fs_close();
  166. return 0;
  167. fail:
  168. return 1;
  169. }