fs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <config.h>
  17. #include <errno.h>
  18. #include <common.h>
  19. #include <part.h>
  20. #include <ext4fs.h>
  21. #include <fat.h>
  22. #include <fs.h>
  23. #include <sandboxfs.h>
  24. #include <asm/io.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. static block_dev_desc_t *fs_dev_desc;
  27. static disk_partition_t fs_partition;
  28. static int fs_type = FS_TYPE_ANY;
  29. static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
  30. disk_partition_t *fs_partition)
  31. {
  32. printf("** Unrecognized filesystem type **\n");
  33. return -1;
  34. }
  35. static inline int fs_ls_unsupported(const char *dirname)
  36. {
  37. return -1;
  38. }
  39. static inline int fs_exists_unsupported(const char *filename)
  40. {
  41. return 0;
  42. }
  43. static inline int fs_size_unsupported(const char *filename, loff_t *size)
  44. {
  45. return -1;
  46. }
  47. static inline int fs_read_unsupported(const char *filename, void *buf,
  48. loff_t offset, loff_t len,
  49. loff_t *actread)
  50. {
  51. return -1;
  52. }
  53. static inline int fs_write_unsupported(const char *filename, void *buf,
  54. loff_t offset, loff_t len,
  55. loff_t *actwrite)
  56. {
  57. return -1;
  58. }
  59. static inline void fs_close_unsupported(void)
  60. {
  61. }
  62. static inline int fs_uuid_unsupported(char *uuid_str)
  63. {
  64. return -1;
  65. }
  66. struct fstype_info {
  67. int fstype;
  68. /*
  69. * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
  70. * should be false in most cases. For "virtual" filesystems which
  71. * aren't based on a U-Boot block device (e.g. sandbox), this can be
  72. * set to true. This should also be true for the dumm entry at the end
  73. * of fstypes[], since that is essentially a "virtual" (non-existent)
  74. * filesystem.
  75. */
  76. bool null_dev_desc_ok;
  77. int (*probe)(block_dev_desc_t *fs_dev_desc,
  78. disk_partition_t *fs_partition);
  79. int (*ls)(const char *dirname);
  80. int (*exists)(const char *filename);
  81. int (*size)(const char *filename, loff_t *size);
  82. int (*read)(const char *filename, void *buf, loff_t offset,
  83. loff_t len, loff_t *actread);
  84. int (*write)(const char *filename, void *buf, loff_t offset,
  85. loff_t len, loff_t *actwrite);
  86. void (*close)(void);
  87. int (*uuid)(char *uuid_str);
  88. };
  89. static struct fstype_info fstypes[] = {
  90. #ifdef CONFIG_FS_FAT
  91. {
  92. .fstype = FS_TYPE_FAT,
  93. .null_dev_desc_ok = false,
  94. .probe = fat_set_blk_dev,
  95. .close = fat_close,
  96. .ls = file_fat_ls,
  97. .exists = fat_exists,
  98. .size = fat_size,
  99. .read = fat_read_file,
  100. #ifdef CONFIG_FAT_WRITE
  101. .write = file_fat_write,
  102. #else
  103. .write = fs_write_unsupported,
  104. #endif
  105. .uuid = fs_uuid_unsupported,
  106. },
  107. #endif
  108. #ifdef CONFIG_FS_EXT4
  109. {
  110. .fstype = FS_TYPE_EXT,
  111. .null_dev_desc_ok = false,
  112. .probe = ext4fs_probe,
  113. .close = ext4fs_close,
  114. .ls = ext4fs_ls,
  115. .exists = ext4fs_exists,
  116. .size = ext4fs_size,
  117. .read = ext4_read_file,
  118. #ifdef CONFIG_CMD_EXT4_WRITE
  119. .write = ext4_write_file,
  120. #else
  121. .write = fs_write_unsupported,
  122. #endif
  123. .uuid = ext4fs_uuid,
  124. },
  125. #endif
  126. #ifdef CONFIG_SANDBOX
  127. {
  128. .fstype = FS_TYPE_SANDBOX,
  129. .null_dev_desc_ok = true,
  130. .probe = sandbox_fs_set_blk_dev,
  131. .close = sandbox_fs_close,
  132. .ls = sandbox_fs_ls,
  133. .exists = sandbox_fs_exists,
  134. .size = sandbox_fs_size,
  135. .read = fs_read_sandbox,
  136. .write = fs_write_sandbox,
  137. .uuid = fs_uuid_unsupported,
  138. },
  139. #endif
  140. {
  141. .fstype = FS_TYPE_ANY,
  142. .null_dev_desc_ok = true,
  143. .probe = fs_probe_unsupported,
  144. .close = fs_close_unsupported,
  145. .ls = fs_ls_unsupported,
  146. .exists = fs_exists_unsupported,
  147. .size = fs_size_unsupported,
  148. .read = fs_read_unsupported,
  149. .write = fs_write_unsupported,
  150. .uuid = fs_uuid_unsupported,
  151. },
  152. };
  153. static struct fstype_info *fs_get_info(int fstype)
  154. {
  155. struct fstype_info *info;
  156. int i;
  157. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  158. if (fstype == info->fstype)
  159. return info;
  160. }
  161. /* Return the 'unsupported' sentinel */
  162. return info;
  163. }
  164. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  165. {
  166. struct fstype_info *info;
  167. int part, i;
  168. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  169. static int relocated;
  170. if (!relocated) {
  171. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  172. i++, info++) {
  173. info->probe += gd->reloc_off;
  174. info->close += gd->reloc_off;
  175. info->ls += gd->reloc_off;
  176. info->read += gd->reloc_off;
  177. info->write += gd->reloc_off;
  178. }
  179. relocated = 1;
  180. }
  181. #endif
  182. part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
  183. &fs_partition, 1);
  184. if (part < 0)
  185. return -1;
  186. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  187. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  188. fstype != info->fstype)
  189. continue;
  190. if (!fs_dev_desc && !info->null_dev_desc_ok)
  191. continue;
  192. if (!info->probe(fs_dev_desc, &fs_partition)) {
  193. fs_type = info->fstype;
  194. return 0;
  195. }
  196. }
  197. return -1;
  198. }
  199. static void fs_close(void)
  200. {
  201. struct fstype_info *info = fs_get_info(fs_type);
  202. info->close();
  203. fs_type = FS_TYPE_ANY;
  204. }
  205. int fs_uuid(char *uuid_str)
  206. {
  207. struct fstype_info *info = fs_get_info(fs_type);
  208. return info->uuid(uuid_str);
  209. }
  210. int fs_ls(const char *dirname)
  211. {
  212. int ret;
  213. struct fstype_info *info = fs_get_info(fs_type);
  214. ret = info->ls(dirname);
  215. fs_type = FS_TYPE_ANY;
  216. fs_close();
  217. return ret;
  218. }
  219. int fs_exists(const char *filename)
  220. {
  221. int ret;
  222. struct fstype_info *info = fs_get_info(fs_type);
  223. ret = info->exists(filename);
  224. fs_close();
  225. return ret;
  226. }
  227. int fs_size(const char *filename, loff_t *size)
  228. {
  229. int ret;
  230. struct fstype_info *info = fs_get_info(fs_type);
  231. ret = info->size(filename, size);
  232. fs_close();
  233. return ret;
  234. }
  235. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  236. loff_t *actread)
  237. {
  238. struct fstype_info *info = fs_get_info(fs_type);
  239. void *buf;
  240. int ret;
  241. /*
  242. * We don't actually know how many bytes are being read, since len==0
  243. * means read the whole file.
  244. */
  245. buf = map_sysmem(addr, len);
  246. ret = info->read(filename, buf, offset, len, actread);
  247. unmap_sysmem(buf);
  248. /* If we requested a specific number of bytes, check we got it */
  249. if (ret == 0 && len && *actread != len) {
  250. printf("** Unable to read file %s **\n", filename);
  251. ret = -1;
  252. }
  253. fs_close();
  254. return ret;
  255. }
  256. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  257. loff_t *actwrite)
  258. {
  259. struct fstype_info *info = fs_get_info(fs_type);
  260. void *buf;
  261. int ret;
  262. buf = map_sysmem(addr, len);
  263. ret = info->write(filename, buf, offset, len, actwrite);
  264. unmap_sysmem(buf);
  265. if (ret < 0 && len != *actwrite) {
  266. printf("** Unable to write file %s **\n", filename);
  267. ret = -1;
  268. }
  269. fs_close();
  270. return ret;
  271. }
  272. int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  273. int fstype)
  274. {
  275. loff_t size;
  276. if (argc != 4)
  277. return CMD_RET_USAGE;
  278. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  279. return 1;
  280. if (fs_size(argv[3], &size) < 0)
  281. return CMD_RET_FAILURE;
  282. setenv_hex("filesize", size);
  283. return 0;
  284. }
  285. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  286. int fstype)
  287. {
  288. unsigned long addr;
  289. const char *addr_str;
  290. const char *filename;
  291. loff_t bytes;
  292. loff_t pos;
  293. loff_t len_read;
  294. int ret;
  295. unsigned long time;
  296. char *ep;
  297. if (argc < 2)
  298. return CMD_RET_USAGE;
  299. if (argc > 7)
  300. return CMD_RET_USAGE;
  301. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  302. return 1;
  303. if (argc >= 4) {
  304. addr = simple_strtoul(argv[3], &ep, 16);
  305. if (ep == argv[3] || *ep != '\0')
  306. return CMD_RET_USAGE;
  307. } else {
  308. addr_str = getenv("loadaddr");
  309. if (addr_str != NULL)
  310. addr = simple_strtoul(addr_str, NULL, 16);
  311. else
  312. addr = CONFIG_SYS_LOAD_ADDR;
  313. }
  314. if (argc >= 5) {
  315. filename = argv[4];
  316. } else {
  317. filename = getenv("bootfile");
  318. if (!filename) {
  319. puts("** No boot file defined **\n");
  320. return 1;
  321. }
  322. }
  323. if (argc >= 6)
  324. bytes = simple_strtoul(argv[5], NULL, 16);
  325. else
  326. bytes = 0;
  327. if (argc >= 7)
  328. pos = simple_strtoul(argv[6], NULL, 16);
  329. else
  330. pos = 0;
  331. time = get_timer(0);
  332. ret = fs_read(filename, addr, pos, bytes, &len_read);
  333. time = get_timer(time);
  334. if (ret < 0)
  335. return 1;
  336. printf("%llu bytes read in %lu ms", len_read, time);
  337. if (time > 0) {
  338. puts(" (");
  339. print_size(len_read / time * 1000, "/s");
  340. puts(")");
  341. }
  342. puts("\n");
  343. setenv_hex("filesize", len_read);
  344. return 0;
  345. }
  346. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  347. int fstype)
  348. {
  349. if (argc < 2)
  350. return CMD_RET_USAGE;
  351. if (argc > 4)
  352. return CMD_RET_USAGE;
  353. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  354. return 1;
  355. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  356. return 1;
  357. return 0;
  358. }
  359. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  360. int fstype)
  361. {
  362. if (fs_set_blk_dev(dev_type, dev_part, fstype))
  363. return 0;
  364. return fs_exists(file);
  365. }
  366. int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  367. int fstype)
  368. {
  369. unsigned long addr;
  370. const char *filename;
  371. loff_t bytes;
  372. loff_t pos;
  373. loff_t len;
  374. int ret;
  375. unsigned long time;
  376. if (argc < 6 || argc > 7)
  377. return CMD_RET_USAGE;
  378. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  379. return 1;
  380. addr = simple_strtoul(argv[3], NULL, 16);
  381. filename = argv[4];
  382. bytes = simple_strtoul(argv[5], NULL, 16);
  383. if (argc >= 7)
  384. pos = simple_strtoul(argv[6], NULL, 16);
  385. else
  386. pos = 0;
  387. time = get_timer(0);
  388. ret = fs_write(filename, addr, pos, bytes, &len);
  389. time = get_timer(time);
  390. if (ret < 0)
  391. return 1;
  392. printf("%llu bytes written in %lu ms", len, time);
  393. if (time > 0) {
  394. puts(" (");
  395. print_size(len / time * 1000, "/s");
  396. puts(")");
  397. }
  398. puts("\n");
  399. return 0;
  400. }
  401. int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  402. int fstype)
  403. {
  404. int ret;
  405. char uuid[37];
  406. memset(uuid, 0, sizeof(uuid));
  407. if (argc < 3 || argc > 4)
  408. return CMD_RET_USAGE;
  409. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  410. return 1;
  411. ret = fs_uuid(uuid);
  412. if (ret)
  413. return CMD_RET_FAILURE;
  414. if (argc == 4)
  415. setenv(argv[3], uuid);
  416. else
  417. printf("%s\n", uuid);
  418. return CMD_RET_SUCCESS;
  419. }