fs.c 11 KB

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