cramfs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * based on: cmd_jffs2.c
  5. *
  6. * Add support for a CRAMFS located in RAM
  7. */
  8. /*
  9. * CRAMFS support
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <mapmem.h>
  15. #include <linux/list.h>
  16. #include <linux/ctype.h>
  17. #include <jffs2/jffs2.h>
  18. #include <jffs2/load_kernel.h>
  19. #include <cramfs/cramfs_fs.h>
  20. #include <asm/io.h>
  21. /* enable/disable debugging messages */
  22. #define DEBUG_CRAMFS
  23. #undef DEBUG_CRAMFS
  24. #ifdef DEBUG_CRAMFS
  25. # define DEBUGF(fmt, args...) printf(fmt ,##args)
  26. #else
  27. # define DEBUGF(fmt, args...)
  28. #endif
  29. #include <flash.h>
  30. #ifndef CONFIG_MTD_NOR_FLASH
  31. # define OFFSET_ADJUSTMENT 0
  32. #else
  33. # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
  34. #endif
  35. #ifndef CONFIG_FS_JFFS2
  36. #include <linux/stat.h>
  37. char *mkmodestr(unsigned long mode, char *str)
  38. {
  39. static const char *l = "xwr";
  40. int mask = 1, i;
  41. char c;
  42. switch (mode & S_IFMT) {
  43. case S_IFDIR: str[0] = 'd'; break;
  44. case S_IFBLK: str[0] = 'b'; break;
  45. case S_IFCHR: str[0] = 'c'; break;
  46. case S_IFIFO: str[0] = 'f'; break;
  47. case S_IFLNK: str[0] = 'l'; break;
  48. case S_IFSOCK: str[0] = 's'; break;
  49. case S_IFREG: str[0] = '-'; break;
  50. default: str[0] = '?';
  51. }
  52. for(i = 0; i < 9; i++) {
  53. c = l[i%3];
  54. str[9-i] = (mode & mask)?c:'-';
  55. mask = mask<<1;
  56. }
  57. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  58. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  59. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  60. str[10] = '\0';
  61. return str;
  62. }
  63. #endif /* CONFIG_FS_JFFS2 */
  64. extern int cramfs_check (struct part_info *info);
  65. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  66. extern int cramfs_ls (struct part_info *info, char *filename);
  67. extern int cramfs_info (struct part_info *info);
  68. /***************************************************/
  69. /* U-Boot commands */
  70. /***************************************************/
  71. /**
  72. * Routine implementing fsload u-boot command. This routine tries to load
  73. * a requested file from cramfs filesystem at location 'cramfsaddr'.
  74. * cramfsaddr is an evironment variable.
  75. *
  76. * @param cmdtp command internal data
  77. * @param flag command flag
  78. * @param argc number of arguments supplied to the command
  79. * @param argv arguments list
  80. * @return 0 on success, 1 otherwise
  81. */
  82. int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  83. {
  84. char *filename;
  85. int size;
  86. ulong offset = load_addr;
  87. char *offset_virt;
  88. struct part_info part;
  89. struct mtd_device dev;
  90. struct mtdids id;
  91. ulong addr;
  92. addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
  93. /* hack! */
  94. /* cramfs_* only supports NOR flash chips */
  95. /* fake the device type */
  96. id.type = MTD_DEV_TYPE_NOR;
  97. id.num = 0;
  98. dev.id = &id;
  99. part.dev = &dev;
  100. /* fake the address offset */
  101. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  102. /* pre-set Boot file name */
  103. filename = env_get("bootfile");
  104. if (!filename)
  105. filename = "uImage";
  106. if (argc == 2) {
  107. filename = argv[1];
  108. }
  109. if (argc == 3) {
  110. offset = simple_strtoul(argv[1], NULL, 0);
  111. load_addr = offset;
  112. filename = argv[2];
  113. }
  114. offset_virt = map_sysmem(offset, 0);
  115. size = 0;
  116. if (cramfs_check(&part))
  117. size = cramfs_load (offset_virt, &part, filename);
  118. if (size > 0) {
  119. printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
  120. size, offset);
  121. env_set_hex("filesize", size);
  122. } else {
  123. printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
  124. }
  125. unmap_sysmem(offset_virt);
  126. unmap_sysmem((void *)(uintptr_t)part.offset);
  127. return !(size > 0);
  128. }
  129. /**
  130. * Routine implementing u-boot ls command which lists content of a given
  131. * directory at location 'cramfsaddr'.
  132. * cramfsaddr is an evironment variable.
  133. *
  134. * @param cmdtp command internal data
  135. * @param flag command flag
  136. * @param argc number of arguments supplied to the command
  137. * @param argv arguments list
  138. * @return 0 on success, 1 otherwise
  139. */
  140. int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  141. {
  142. char *filename = "/";
  143. int ret;
  144. struct part_info part;
  145. struct mtd_device dev;
  146. struct mtdids id;
  147. ulong addr;
  148. addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
  149. /* hack! */
  150. /* cramfs_* only supports NOR flash chips */
  151. /* fake the device type */
  152. id.type = MTD_DEV_TYPE_NOR;
  153. id.num = 0;
  154. dev.id = &id;
  155. part.dev = &dev;
  156. /* fake the address offset */
  157. part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
  158. if (argc == 2)
  159. filename = argv[1];
  160. ret = 0;
  161. if (cramfs_check(&part))
  162. ret = cramfs_ls (&part, filename);
  163. unmap_sysmem((void *)(uintptr_t)part.offset);
  164. return ret ? 0 : 1;
  165. }
  166. /* command line only */
  167. /***************************************************/
  168. U_BOOT_CMD(
  169. cramfsload, 3, 0, do_cramfs_load,
  170. "load binary file from a filesystem image",
  171. "[ off ] [ filename ]\n"
  172. " - load binary file from address 'cramfsaddr'\n"
  173. " with offset 'off'\n"
  174. );
  175. U_BOOT_CMD(
  176. cramfsls, 2, 1, do_cramfs_ls,
  177. "list files in a directory (default /)",
  178. "[ directory ]\n"
  179. " - list files in a directory.\n"
  180. );