sqfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Bootlin
  4. *
  5. * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
  6. *
  7. * sqfs.c: SquashFS filesystem implementation
  8. */
  9. #include <asm/unaligned.h>
  10. #include <errno.h>
  11. #include <fs.h>
  12. #include <linux/types.h>
  13. #include <linux/byteorder/little_endian.h>
  14. #include <linux/byteorder/generic.h>
  15. #include <memalign.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <squashfs.h>
  19. #include <part.h>
  20. #include "sqfs_decompressor.h"
  21. #include "sqfs_filesystem.h"
  22. #include "sqfs_utils.h"
  23. static struct squashfs_ctxt ctxt;
  24. static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf)
  25. {
  26. ulong ret;
  27. if (!ctxt.cur_dev)
  28. return -1;
  29. ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block,
  30. nr_blocks, buf);
  31. if (ret != nr_blocks)
  32. return -1;
  33. return ret;
  34. }
  35. static int sqfs_read_sblk(struct squashfs_super_block **sblk)
  36. {
  37. *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz);
  38. if (!*sblk)
  39. return -ENOMEM;
  40. if (sqfs_disk_read(0, 1, *sblk) != 1) {
  41. free(*sblk);
  42. return -EINVAL;
  43. }
  44. return 0;
  45. }
  46. static int sqfs_count_tokens(const char *filename)
  47. {
  48. int token_count = 1, l;
  49. for (l = 1; l < strlen(filename); l++) {
  50. if (filename[l] == '/')
  51. token_count++;
  52. }
  53. /* Ignore trailing '/' in path */
  54. if (filename[strlen(filename) - 1] == '/')
  55. token_count--;
  56. if (!token_count)
  57. token_count = 1;
  58. return token_count;
  59. }
  60. /*
  61. * Calculates how many blocks are needed for the buffer used in sqfs_disk_read.
  62. * The memory section (e.g. inode table) start offset and its end (i.e. the next
  63. * table start) must be specified. It also calculates the offset from which to
  64. * start reading the buffer.
  65. */
  66. static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
  67. {
  68. u64 start_, table_size;
  69. table_size = le64_to_cpu(end) - le64_to_cpu(start);
  70. start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz;
  71. *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
  72. return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
  73. }
  74. /*
  75. * Retrieves fragment block entry and returns true if the fragment block is
  76. * compressed
  77. */
  78. static int sqfs_frag_lookup(u32 inode_fragment_index,
  79. struct squashfs_fragment_block_entry *e)
  80. {
  81. u64 start, n_blks, src_len, table_offset, start_block;
  82. unsigned char *metadata_buffer, *metadata, *table;
  83. struct squashfs_fragment_block_entry *entries;
  84. struct squashfs_super_block *sblk = ctxt.sblk;
  85. unsigned long dest_len;
  86. int block, offset, ret;
  87. u16 header;
  88. if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments))
  89. return -EINVAL;
  90. start = get_unaligned_le64(&sblk->fragment_table_start) /
  91. ctxt.cur_dev->blksz;
  92. n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
  93. sblk->export_table_start,
  94. &table_offset);
  95. /* Allocate a proper sized buffer to store the fragment index table */
  96. table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  97. if (!table)
  98. return -ENOMEM;
  99. if (sqfs_disk_read(start, n_blks, table) < 0) {
  100. free(table);
  101. return -EINVAL;
  102. }
  103. block = SQFS_FRAGMENT_INDEX(inode_fragment_index);
  104. offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index);
  105. /*
  106. * Get the start offset of the metadata block that contains the right
  107. * fragment block entry
  108. */
  109. start_block = get_unaligned_le64(table + table_offset + block *
  110. sizeof(u64));
  111. start = start_block / ctxt.cur_dev->blksz;
  112. n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
  113. sblk->fragment_table_start, &table_offset);
  114. metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  115. if (!metadata_buffer) {
  116. ret = -ENOMEM;
  117. goto free_table;
  118. }
  119. if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) {
  120. ret = -EINVAL;
  121. goto free_buffer;
  122. }
  123. /* Every metadata block starts with a 16-bit header */
  124. header = get_unaligned_le16(metadata_buffer + table_offset);
  125. metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE;
  126. if (!metadata || !header) {
  127. ret = -ENOMEM;
  128. goto free_buffer;
  129. }
  130. entries = malloc(SQFS_METADATA_BLOCK_SIZE);
  131. if (!entries) {
  132. ret = -ENOMEM;
  133. goto free_buffer;
  134. }
  135. if (SQFS_COMPRESSED_METADATA(header)) {
  136. src_len = SQFS_METADATA_SIZE(header);
  137. dest_len = SQFS_METADATA_BLOCK_SIZE;
  138. ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata,
  139. src_len);
  140. if (ret) {
  141. ret = -EINVAL;
  142. goto free_entries;
  143. }
  144. } else {
  145. memcpy(entries, metadata, SQFS_METADATA_SIZE(header));
  146. }
  147. *e = entries[offset];
  148. ret = SQFS_COMPRESSED_BLOCK(e->size);
  149. free_entries:
  150. free(entries);
  151. free_buffer:
  152. free(metadata_buffer);
  153. free_table:
  154. free(table);
  155. return ret;
  156. }
  157. /*
  158. * The entry name is a flexible array member, and we don't know its size before
  159. * actually reading the entry. So we need a first copy to retrieve this size so
  160. * we can finally copy the whole struct.
  161. */
  162. static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src)
  163. {
  164. struct squashfs_directory_entry *tmp;
  165. u16 sz;
  166. tmp = src;
  167. sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16));
  168. /*
  169. * 'src' points to the begin of a directory entry, and 'sz' gets its
  170. * 'name_size' member's value. name_size is actually the string
  171. * length - 1, so adding 2 compensates this difference and adds space
  172. * for the trailling null byte.
  173. */
  174. *dest = malloc(sizeof(*tmp) + sz + 2);
  175. if (!*dest)
  176. return -ENOMEM;
  177. memcpy(*dest, src, sizeof(*tmp) + sz + 1);
  178. (*dest)->name[sz + 1] = '\0';
  179. return 0;
  180. }
  181. static int sqfs_get_tokens_length(char **tokens, int count)
  182. {
  183. int length = 0, i;
  184. /*
  185. * 1 is added to the result of strlen to consider the slash separator
  186. * between the tokens.
  187. */
  188. for (i = 0; i < count; i++)
  189. length += strlen(tokens[i]) + 1;
  190. return length;
  191. }
  192. /* Takes a token list and returns a single string with '/' as separator. */
  193. static char *sqfs_concat_tokens(char **token_list, int token_count)
  194. {
  195. char *result;
  196. int i, length = 0, offset = 0;
  197. length = sqfs_get_tokens_length(token_list, token_count);
  198. result = malloc(length + 1);
  199. result[length] = '\0';
  200. for (i = 0; i < token_count; i++) {
  201. strcpy(result + offset, token_list[i]);
  202. offset += strlen(token_list[i]);
  203. result[offset++] = '/';
  204. }
  205. return result;
  206. }
  207. /*
  208. * Differently from sqfs_concat_tokens, sqfs_join writes the result into a
  209. * previously allocated string, and returns the number of bytes written.
  210. */
  211. static int sqfs_join(char **strings, char *dest, int start, int end,
  212. char separator)
  213. {
  214. int i, offset = 0;
  215. for (i = start; i < end; i++) {
  216. strcpy(dest + offset, strings[i]);
  217. offset += strlen(strings[i]);
  218. if (i < end - 1)
  219. dest[offset++] = separator;
  220. }
  221. return offset;
  222. }
  223. /*
  224. * Fills the given token list using its size (count) and a source string (str)
  225. */
  226. static int sqfs_tokenize(char **tokens, int count, const char *str)
  227. {
  228. int i, j, ret = 0;
  229. char *aux, *strc;
  230. strc = strdup(str);
  231. if (!strc)
  232. return -ENOMEM;
  233. if (!strcmp(strc, "/")) {
  234. tokens[0] = strdup(strc);
  235. if (!tokens[0]) {
  236. ret = -ENOMEM;
  237. goto free_strc;
  238. }
  239. } else {
  240. for (j = 0; j < count; j++) {
  241. aux = strtok(!j ? strc : NULL, "/");
  242. tokens[j] = strdup(aux);
  243. if (!tokens[j]) {
  244. for (i = 0; i < j; i++)
  245. free(tokens[i]);
  246. ret = -ENOMEM;
  247. goto free_strc;
  248. }
  249. }
  250. }
  251. free_strc:
  252. free(strc);
  253. return ret;
  254. }
  255. /*
  256. * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us
  257. * with a token list containing only the tokens needed to form the resolved
  258. * path, and returns the decremented size of the token list.
  259. */
  260. static int sqfs_clean_base_path(char **base, int count, int updir)
  261. {
  262. int i;
  263. for (i = count - updir - 1; i < count; i++)
  264. free(base[i]);
  265. return count - updir - 1;
  266. }
  267. /*
  268. * Given the base ("current dir.") path and the relative one, generate the
  269. * absolute path.
  270. */
  271. static char *sqfs_get_abs_path(const char *base, const char *rel)
  272. {
  273. char **base_tokens, **rel_tokens, *resolved = NULL;
  274. int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0;
  275. /* Memory allocation for the token lists */
  276. bc = sqfs_count_tokens(base);
  277. rc = sqfs_count_tokens(rel);
  278. if (bc < 1 || rc < 1)
  279. return NULL;
  280. base_tokens = malloc(bc * sizeof(char *));
  281. if (!base_tokens)
  282. return NULL;
  283. rel_tokens = malloc(rc * sizeof(char *));
  284. if (!rel_tokens)
  285. goto free_b_tokens;
  286. /* Fill token lists */
  287. ret = sqfs_tokenize(base_tokens, bc, base);
  288. if (ret)
  289. goto free_r_tokens;
  290. sqfs_tokenize(rel_tokens, rc, rel);
  291. if (ret)
  292. goto free_r_tokens;
  293. /* count '..' occurrences in target path */
  294. for (i = 0; i < rc; i++) {
  295. if (!strcmp(rel_tokens[i], ".."))
  296. updir++;
  297. }
  298. /* Remove the last token and the '..' occurrences */
  299. bc = sqfs_clean_base_path(base_tokens, bc, updir);
  300. if (bc < 0)
  301. goto free_r_tokens;
  302. /* Calculate resolved path size */
  303. if (!bc)
  304. resolved_size++;
  305. resolved_size += sqfs_get_tokens_length(base_tokens, bc) +
  306. sqfs_get_tokens_length(rel_tokens, rc);
  307. resolved = malloc(resolved_size + 1);
  308. if (!resolved)
  309. goto free_r_tokens_loop;
  310. /* Set resolved path */
  311. memset(resolved, '\0', resolved_size + 1);
  312. offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/');
  313. resolved[offset++] = '/';
  314. offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/');
  315. free_r_tokens_loop:
  316. for (i = 0; i < rc; i++)
  317. free(rel_tokens[i]);
  318. for (i = 0; i < bc; i++)
  319. free(base_tokens[i]);
  320. free_r_tokens:
  321. free(rel_tokens);
  322. free_b_tokens:
  323. free(base_tokens);
  324. return resolved;
  325. }
  326. static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym,
  327. const char *base_path)
  328. {
  329. char *resolved, *target;
  330. u32 sz;
  331. sz = get_unaligned_le32(&sym->symlink_size);
  332. target = malloc(sz + 1);
  333. if (!target)
  334. return NULL;
  335. /*
  336. * There is no trailling null byte in the symlink's target path, so a
  337. * copy is made and a '\0' is added at its end.
  338. */
  339. target[sz] = '\0';
  340. /* Get target name (relative path) */
  341. strncpy(target, sym->symlink, sz);
  342. /* Relative -> absolute path conversion */
  343. resolved = sqfs_get_abs_path(base_path, target);
  344. free(target);
  345. return resolved;
  346. }
  347. /*
  348. * m_list contains each metadata block's position, and m_count is the number of
  349. * elements of m_list. Those metadata blocks come from the compressed directory
  350. * table.
  351. */
  352. static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
  353. int token_count, u32 *m_list, int m_count)
  354. {
  355. struct squashfs_super_block *sblk = ctxt.sblk;
  356. char *path, *target, **sym_tokens, *res, *rem;
  357. int j, ret, new_inode_number, offset;
  358. struct squashfs_symlink_inode *sym;
  359. struct squashfs_ldir_inode *ldir;
  360. struct squashfs_dir_inode *dir;
  361. struct fs_dir_stream *dirsp;
  362. struct fs_dirent *dent;
  363. unsigned char *table;
  364. dirsp = (struct fs_dir_stream *)dirs;
  365. /* Start by root inode */
  366. table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes),
  367. sblk->inodes, sblk->block_size);
  368. dir = (struct squashfs_dir_inode *)table;
  369. ldir = (struct squashfs_ldir_inode *)table;
  370. /* get directory offset in directory table */
  371. offset = sqfs_dir_offset(table, m_list, m_count);
  372. dirs->table = &dirs->dir_table[offset];
  373. /* Setup directory header */
  374. dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE);
  375. if (!dirs->dir_header)
  376. return -ENOMEM;
  377. memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
  378. /* Initialize squashfs_dir_stream members */
  379. dirs->table += SQFS_DIR_HEADER_SIZE;
  380. dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
  381. dirs->entry_count = dirs->dir_header->count + 1;
  382. /* No path given -> root directory */
  383. if (!strcmp(token_list[0], "/")) {
  384. dirs->table = &dirs->dir_table[offset];
  385. memcpy(&dirs->i_dir, dir, sizeof(*dir));
  386. return 0;
  387. }
  388. for (j = 0; j < token_count; j++) {
  389. if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
  390. printf("** Cannot find directory. **\n");
  391. return -EINVAL;
  392. }
  393. while (!sqfs_readdir(dirsp, &dent)) {
  394. ret = strcmp(dent->name, token_list[j]);
  395. if (!ret)
  396. break;
  397. free(dirs->entry);
  398. }
  399. if (ret) {
  400. printf("** Cannot find directory. **\n");
  401. return -EINVAL;
  402. }
  403. /* Redefine inode as the found token */
  404. new_inode_number = dirs->entry->inode_offset +
  405. dirs->dir_header->inode_number;
  406. /* Get reference to inode in the inode table */
  407. table = sqfs_find_inode(dirs->inode_table, new_inode_number,
  408. sblk->inodes, sblk->block_size);
  409. dir = (struct squashfs_dir_inode *)table;
  410. /* Check for symbolic link and inode type sanity */
  411. if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) {
  412. sym = (struct squashfs_symlink_inode *)table;
  413. /* Get first j + 1 tokens */
  414. path = sqfs_concat_tokens(token_list, j + 1);
  415. /* Resolve for these tokens */
  416. target = sqfs_resolve_symlink(sym, path);
  417. /* Join remaining tokens */
  418. rem = sqfs_concat_tokens(token_list + j + 1, token_count -
  419. j - 1);
  420. /* Concatenate remaining tokens and symlink's target */
  421. res = malloc(strlen(rem) + strlen(target) + 1);
  422. strcpy(res, target);
  423. res[strlen(target)] = '/';
  424. strcpy(res + strlen(target) + 1, rem);
  425. token_count = sqfs_count_tokens(res);
  426. if (token_count < 0)
  427. return -EINVAL;
  428. sym_tokens = malloc(token_count * sizeof(char *));
  429. if (!sym_tokens)
  430. return -EINVAL;
  431. /* Fill tokens list */
  432. ret = sqfs_tokenize(sym_tokens, token_count, res);
  433. if (ret)
  434. return -EINVAL;
  435. free(dirs->entry);
  436. ret = sqfs_search_dir(dirs, sym_tokens, token_count,
  437. m_list, m_count);
  438. return ret;
  439. } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
  440. printf("** Cannot find directory. **\n");
  441. free(dirs->entry);
  442. return -EINVAL;
  443. }
  444. /* Check if it is an extended dir. */
  445. if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE)
  446. ldir = (struct squashfs_ldir_inode *)table;
  447. /* Get dir. offset into the directory table */
  448. offset = sqfs_dir_offset(table, m_list, m_count);
  449. dirs->table = &dirs->dir_table[offset];
  450. /* Copy directory header */
  451. memcpy(dirs->dir_header, &dirs->dir_table[offset],
  452. SQFS_DIR_HEADER_SIZE);
  453. /* Check for empty directory */
  454. if (sqfs_is_empty_dir(table)) {
  455. printf("Empty directory.\n");
  456. free(dirs->entry);
  457. return SQFS_EMPTY_DIR;
  458. }
  459. dirs->table += SQFS_DIR_HEADER_SIZE;
  460. dirs->size = get_unaligned_le16(&dir->file_size);
  461. dirs->entry_count = dirs->dir_header->count + 1;
  462. dirs->size -= SQFS_DIR_HEADER_SIZE;
  463. free(dirs->entry);
  464. }
  465. offset = sqfs_dir_offset(table, m_list, m_count);
  466. dirs->table = &dirs->dir_table[offset];
  467. if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE)
  468. memcpy(&dirs->i_dir, dir, sizeof(*dir));
  469. else
  470. memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
  471. return 0;
  472. }
  473. /*
  474. * Inode and directory tables are stored as a series of metadata blocks, and
  475. * given the compressed size of this table, we can calculate how much metadata
  476. * blocks are needed to store the result of the decompression, since a
  477. * decompressed metadata block should have a size of 8KiB.
  478. */
  479. static int sqfs_count_metablks(void *table, u32 offset, int table_size)
  480. {
  481. int count = 0, cur_size = 0, ret;
  482. u32 data_size;
  483. bool comp;
  484. do {
  485. ret = sqfs_read_metablock(table, offset + cur_size, &comp,
  486. &data_size);
  487. if (ret)
  488. return -EINVAL;
  489. cur_size += data_size + SQFS_HEADER_SIZE;
  490. count++;
  491. } while (cur_size < table_size);
  492. return count;
  493. }
  494. /*
  495. * Storing the metadata blocks header's positions will be useful while looking
  496. * for an entry in the directory table, using the reference (index and offset)
  497. * given by its inode.
  498. */
  499. static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,
  500. int metablks_count)
  501. {
  502. u32 data_size, cur_size = 0;
  503. int j, ret = 0;
  504. bool comp;
  505. if (!metablks_count)
  506. return -EINVAL;
  507. for (j = 0; j < metablks_count; j++) {
  508. ret = sqfs_read_metablock(table, offset + cur_size, &comp,
  509. &data_size);
  510. if (ret)
  511. return -EINVAL;
  512. cur_size += data_size + SQFS_HEADER_SIZE;
  513. pos_list[j] = cur_size;
  514. }
  515. return ret;
  516. }
  517. static int sqfs_read_inode_table(unsigned char **inode_table)
  518. {
  519. struct squashfs_super_block *sblk = ctxt.sblk;
  520. u64 start, n_blks, table_offset, table_size;
  521. int j, ret = 0, metablks_count;
  522. unsigned char *src_table, *itb;
  523. u32 src_len, dest_offset = 0;
  524. unsigned long dest_len = 0;
  525. bool compressed;
  526. table_size = get_unaligned_le64(&sblk->directory_table_start) -
  527. get_unaligned_le64(&sblk->inode_table_start);
  528. start = get_unaligned_le64(&sblk->inode_table_start) /
  529. ctxt.cur_dev->blksz;
  530. n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
  531. sblk->directory_table_start, &table_offset);
  532. /* Allocate a proper sized buffer (itb) to store the inode table */
  533. itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  534. if (!itb)
  535. return -ENOMEM;
  536. if (sqfs_disk_read(start, n_blks, itb) < 0) {
  537. ret = -EINVAL;
  538. goto free_itb;
  539. }
  540. /* Parse inode table (metadata block) header */
  541. ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
  542. if (ret) {
  543. ret = -EINVAL;
  544. goto free_itb;
  545. }
  546. /* Calculate size to store the whole decompressed table */
  547. metablks_count = sqfs_count_metablks(itb, table_offset, table_size);
  548. if (metablks_count < 1) {
  549. ret = -EINVAL;
  550. goto free_itb;
  551. }
  552. *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
  553. if (!*inode_table) {
  554. ret = -ENOMEM;
  555. goto free_itb;
  556. }
  557. src_table = itb + table_offset + SQFS_HEADER_SIZE;
  558. /* Extract compressed Inode table */
  559. for (j = 0; j < metablks_count; j++) {
  560. sqfs_read_metablock(itb, table_offset, &compressed, &src_len);
  561. if (compressed) {
  562. dest_len = SQFS_METADATA_BLOCK_SIZE;
  563. ret = sqfs_decompress(&ctxt, *inode_table +
  564. dest_offset, &dest_len,
  565. src_table, src_len);
  566. if (ret) {
  567. free(*inode_table);
  568. goto free_itb;
  569. }
  570. dest_offset += dest_len;
  571. } else {
  572. memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE),
  573. src_table, src_len);
  574. }
  575. /*
  576. * Offsets to the decompression destination, to the metadata
  577. * buffer 'itb' and to the decompression source, respectively.
  578. */
  579. table_offset += src_len + SQFS_HEADER_SIZE;
  580. src_table += src_len + SQFS_HEADER_SIZE;
  581. }
  582. free_itb:
  583. free(itb);
  584. return ret;
  585. }
  586. static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
  587. {
  588. u64 start, n_blks, table_offset, table_size;
  589. struct squashfs_super_block *sblk = ctxt.sblk;
  590. int j, ret = 0, metablks_count = -1;
  591. unsigned char *src_table, *dtb;
  592. u32 src_len, dest_offset = 0;
  593. unsigned long dest_len = 0;
  594. bool compressed;
  595. /* DIRECTORY TABLE */
  596. table_size = get_unaligned_le64(&sblk->fragment_table_start) -
  597. get_unaligned_le64(&sblk->directory_table_start);
  598. start = get_unaligned_le64(&sblk->directory_table_start) /
  599. ctxt.cur_dev->blksz;
  600. n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
  601. sblk->fragment_table_start, &table_offset);
  602. /* Allocate a proper sized buffer (dtb) to store the directory table */
  603. dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  604. if (!dtb)
  605. return -ENOMEM;
  606. if (sqfs_disk_read(start, n_blks, dtb) < 0)
  607. goto free_dtb;
  608. /* Parse directory table (metadata block) header */
  609. ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
  610. if (ret)
  611. goto free_dtb;
  612. /* Calculate total size to store the whole decompressed table */
  613. metablks_count = sqfs_count_metablks(dtb, table_offset, table_size);
  614. if (metablks_count < 1)
  615. goto free_dtb;
  616. *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE);
  617. if (!*dir_table)
  618. goto free_dtb;
  619. *pos_list = malloc(metablks_count * sizeof(u32));
  620. if (!*pos_list) {
  621. free(*dir_table);
  622. goto free_dtb;
  623. }
  624. ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset,
  625. metablks_count);
  626. if (ret) {
  627. metablks_count = -1;
  628. free(*dir_table);
  629. free(*pos_list);
  630. goto free_dtb;
  631. }
  632. src_table = dtb + table_offset + SQFS_HEADER_SIZE;
  633. /* Extract compressed Directory table */
  634. dest_offset = 0;
  635. for (j = 0; j < metablks_count; j++) {
  636. sqfs_read_metablock(dtb, table_offset, &compressed, &src_len);
  637. if (compressed) {
  638. dest_len = SQFS_METADATA_BLOCK_SIZE;
  639. ret = sqfs_decompress(&ctxt, *dir_table +
  640. (j * SQFS_METADATA_BLOCK_SIZE),
  641. &dest_len, src_table, src_len);
  642. if (ret) {
  643. metablks_count = -1;
  644. free(*dir_table);
  645. goto free_dtb;
  646. }
  647. if (dest_len < SQFS_METADATA_BLOCK_SIZE) {
  648. dest_offset += dest_len;
  649. break;
  650. }
  651. dest_offset += dest_len;
  652. } else {
  653. memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE),
  654. src_table, src_len);
  655. }
  656. /*
  657. * Offsets to the decompression destination, to the metadata
  658. * buffer 'dtb' and to the decompression source, respectively.
  659. */
  660. table_offset += src_len + SQFS_HEADER_SIZE;
  661. src_table += src_len + SQFS_HEADER_SIZE;
  662. }
  663. free_dtb:
  664. free(dtb);
  665. return metablks_count;
  666. }
  667. int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp)
  668. {
  669. unsigned char *inode_table = NULL, *dir_table = NULL;
  670. int j, token_count, ret = 0, metablks_count;
  671. struct squashfs_dir_stream *dirs;
  672. char **token_list, *path;
  673. u32 *pos_list = NULL;
  674. dirs = malloc(sizeof(*dirs));
  675. if (!dirs)
  676. return -EINVAL;
  677. ret = sqfs_read_inode_table(&inode_table);
  678. if (ret)
  679. return -EINVAL;
  680. metablks_count = sqfs_read_directory_table(&dir_table, &pos_list);
  681. if (metablks_count < 1)
  682. return -EINVAL;
  683. /* Tokenize filename */
  684. token_count = sqfs_count_tokens(filename);
  685. if (token_count < 0)
  686. return -EINVAL;
  687. path = strdup(filename);
  688. if (!path)
  689. return -ENOMEM;
  690. token_list = malloc(token_count * sizeof(char *));
  691. if (!token_list) {
  692. ret = -EINVAL;
  693. goto free_path;
  694. }
  695. /* Fill tokens list */
  696. ret = sqfs_tokenize(token_list, token_count, path);
  697. if (ret)
  698. goto free_tokens;
  699. /*
  700. * ldir's (extended directory) size is greater than dir, so it works as
  701. * a general solution for the malloc size, since 'i' is a union.
  702. */
  703. dirs->inode_table = inode_table;
  704. dirs->dir_table = dir_table;
  705. ret = sqfs_search_dir(dirs, token_list, token_count, pos_list,
  706. metablks_count);
  707. if (ret)
  708. goto free_tokens;
  709. if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE)
  710. dirs->size = le16_to_cpu(dirs->i_dir.file_size);
  711. else
  712. dirs->size = le32_to_cpu(dirs->i_ldir.file_size);
  713. /* Setup directory header */
  714. memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
  715. dirs->entry_count = dirs->dir_header->count + 1;
  716. dirs->size -= SQFS_DIR_HEADER_SIZE;
  717. /* Setup entry */
  718. dirs->entry = NULL;
  719. dirs->table += SQFS_DIR_HEADER_SIZE;
  720. *dirsp = (struct fs_dir_stream *)dirs;
  721. free_tokens:
  722. for (j = 0; j < token_count; j++)
  723. free(token_list[j]);
  724. free(token_list);
  725. free(pos_list);
  726. free_path:
  727. free(path);
  728. return ret;
  729. }
  730. int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
  731. {
  732. struct squashfs_super_block *sblk = ctxt.sblk;
  733. struct squashfs_dir_stream *dirs;
  734. struct squashfs_lreg_inode *lreg;
  735. struct squashfs_base_inode *base;
  736. struct squashfs_reg_inode *reg;
  737. int i_number, offset = 0, ret;
  738. struct fs_dirent *dent;
  739. unsigned char *ipos;
  740. dirs = (struct squashfs_dir_stream *)fs_dirs;
  741. if (!dirs->size) {
  742. *dentp = NULL;
  743. return -SQFS_STOP_READDIR;
  744. }
  745. dent = &dirs->dentp;
  746. if (!dirs->entry_count) {
  747. if (dirs->size > SQFS_DIR_HEADER_SIZE) {
  748. dirs->size -= SQFS_DIR_HEADER_SIZE;
  749. } else {
  750. *dentp = NULL;
  751. dirs->size = 0;
  752. return -SQFS_STOP_READDIR;
  753. }
  754. if (dirs->size > SQFS_EMPTY_FILE_SIZE) {
  755. /* Read follow-up (emitted) dir. header */
  756. memcpy(dirs->dir_header, dirs->table,
  757. SQFS_DIR_HEADER_SIZE);
  758. dirs->entry_count = dirs->dir_header->count + 1;
  759. ret = sqfs_read_entry(&dirs->entry, dirs->table +
  760. SQFS_DIR_HEADER_SIZE);
  761. if (ret)
  762. return -SQFS_STOP_READDIR;
  763. dirs->table += SQFS_DIR_HEADER_SIZE;
  764. }
  765. } else {
  766. ret = sqfs_read_entry(&dirs->entry, dirs->table);
  767. if (ret)
  768. return -SQFS_STOP_READDIR;
  769. }
  770. i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
  771. ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
  772. sblk->block_size);
  773. base = (struct squashfs_base_inode *)ipos;
  774. /* Set entry type and size */
  775. switch (dirs->entry->type) {
  776. case SQFS_DIR_TYPE:
  777. case SQFS_LDIR_TYPE:
  778. dent->type = FS_DT_DIR;
  779. break;
  780. case SQFS_REG_TYPE:
  781. case SQFS_LREG_TYPE:
  782. /*
  783. * Entries do not differentiate extended from regular types, so
  784. * it needs to be verified manually.
  785. */
  786. if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) {
  787. lreg = (struct squashfs_lreg_inode *)ipos;
  788. dent->size = get_unaligned_le64(&lreg->file_size);
  789. } else {
  790. reg = (struct squashfs_reg_inode *)ipos;
  791. dent->size = get_unaligned_le32(&reg->file_size);
  792. }
  793. dent->type = FS_DT_REG;
  794. break;
  795. case SQFS_BLKDEV_TYPE:
  796. case SQFS_CHRDEV_TYPE:
  797. case SQFS_LBLKDEV_TYPE:
  798. case SQFS_LCHRDEV_TYPE:
  799. case SQFS_FIFO_TYPE:
  800. case SQFS_SOCKET_TYPE:
  801. case SQFS_LFIFO_TYPE:
  802. case SQFS_LSOCKET_TYPE:
  803. dent->type = SQFS_MISC_ENTRY_TYPE;
  804. break;
  805. case SQFS_SYMLINK_TYPE:
  806. case SQFS_LSYMLINK_TYPE:
  807. dent->type = FS_DT_LNK;
  808. break;
  809. default:
  810. return -SQFS_STOP_READDIR;
  811. }
  812. /* Set entry name */
  813. strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1);
  814. dent->name[dirs->entry->name_size + 1] = '\0';
  815. offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
  816. dirs->entry_count--;
  817. /* Decrement size to be read */
  818. if (dirs->size > offset)
  819. dirs->size -= offset;
  820. else
  821. dirs->size = 0;
  822. /* Keep a reference to the current entry before incrementing it */
  823. dirs->table += offset;
  824. *dentp = dent;
  825. return 0;
  826. }
  827. int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition)
  828. {
  829. struct squashfs_super_block *sblk;
  830. int ret;
  831. ctxt.cur_dev = fs_dev_desc;
  832. ctxt.cur_part_info = *fs_partition;
  833. ret = sqfs_read_sblk(&sblk);
  834. if (ret)
  835. return ret;
  836. /* Make sure it has a valid SquashFS magic number*/
  837. if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) {
  838. printf("Bad magic number for SquashFS image.\n");
  839. ctxt.cur_dev = NULL;
  840. return -EINVAL;
  841. }
  842. ctxt.sblk = sblk;
  843. ret = sqfs_decompressor_init(&ctxt);
  844. if (ret) {
  845. ctxt.cur_dev = NULL;
  846. free(ctxt.sblk);
  847. return -EINVAL;
  848. }
  849. return 0;
  850. }
  851. static char *sqfs_basename(char *path)
  852. {
  853. char *fname;
  854. fname = path + strlen(path) - 1;
  855. while (fname >= path) {
  856. if (*fname == '/') {
  857. fname++;
  858. break;
  859. }
  860. fname--;
  861. }
  862. return fname;
  863. }
  864. static char *sqfs_dirname(char *path)
  865. {
  866. char *fname;
  867. fname = sqfs_basename(path);
  868. --fname;
  869. *fname = '\0';
  870. return path;
  871. }
  872. /*
  873. * Takes a path to file and splits it in two parts: the filename itself and the
  874. * directory's path, e.g.:
  875. * path: /path/to/file.txt
  876. * file: file.txt
  877. * dir: /path/to
  878. */
  879. static int sqfs_split_path(char **file, char **dir, const char *path)
  880. {
  881. char *dirc, *basec, *bname, *dname, *tmp_path;
  882. int ret = 0;
  883. /* check for first slash in path*/
  884. if (path[0] == '/') {
  885. tmp_path = strdup(path);
  886. if (!tmp_path)
  887. return -ENOMEM;
  888. } else {
  889. tmp_path = malloc(strlen(path) + 2);
  890. if (!tmp_path)
  891. return -ENOMEM;
  892. tmp_path[0] = '/';
  893. strcpy(tmp_path + 1, path);
  894. }
  895. /* String duplicates */
  896. dirc = strdup(tmp_path);
  897. if (!dirc) {
  898. ret = -ENOMEM;
  899. goto free_tmp;
  900. }
  901. basec = strdup(tmp_path);
  902. if (!basec) {
  903. ret = -ENOMEM;
  904. goto free_dirc;
  905. }
  906. dname = sqfs_dirname(dirc);
  907. bname = sqfs_basename(basec);
  908. *file = strdup(bname);
  909. if (!*file) {
  910. ret = -ENOMEM;
  911. goto free_basec;
  912. }
  913. if (*dname == '\0') {
  914. *dir = malloc(2);
  915. if (!*dir) {
  916. ret = -ENOMEM;
  917. goto free_basec;
  918. }
  919. (*dir)[0] = '/';
  920. (*dir)[1] = '\0';
  921. } else {
  922. *dir = strdup(dname);
  923. if (!*dir) {
  924. ret = -ENOMEM;
  925. goto free_basec;
  926. }
  927. }
  928. free_basec:
  929. free(basec);
  930. free_dirc:
  931. free(dirc);
  932. free_tmp:
  933. free(tmp_path);
  934. return ret;
  935. }
  936. static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
  937. struct squashfs_file_info *finfo,
  938. struct squashfs_fragment_block_entry *fentry,
  939. __le32 blksz)
  940. {
  941. int datablk_count = 0, ret;
  942. finfo->size = get_unaligned_le32(&reg->file_size);
  943. finfo->offset = get_unaligned_le32(&reg->offset);
  944. finfo->start = get_unaligned_le32(&reg->start_block);
  945. finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&reg->fragment));
  946. if (finfo->frag && finfo->offset == 0xFFFFFFFF)
  947. return -EINVAL;
  948. if (finfo->size < 1 || finfo->start == 0xFFFFFFFF)
  949. return -EINVAL;
  950. if (finfo->frag) {
  951. datablk_count = finfo->size / le32_to_cpu(blksz);
  952. ret = sqfs_frag_lookup(get_unaligned_le32(&reg->fragment),
  953. fentry);
  954. if (ret < 0)
  955. return -EINVAL;
  956. finfo->comp = true;
  957. if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
  958. return -EINVAL;
  959. } else {
  960. datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
  961. }
  962. finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
  963. if (!finfo->blk_sizes)
  964. return -ENOMEM;
  965. return datablk_count;
  966. }
  967. static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
  968. struct squashfs_file_info *finfo,
  969. struct squashfs_fragment_block_entry *fentry,
  970. __le32 blksz)
  971. {
  972. int datablk_count = 0, ret;
  973. finfo->size = get_unaligned_le64(&lreg->file_size);
  974. finfo->offset = get_unaligned_le32(&lreg->offset);
  975. finfo->start = get_unaligned_le64(&lreg->start_block);
  976. finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment));
  977. if (finfo->frag && finfo->offset == 0xFFFFFFFF)
  978. return -EINVAL;
  979. if (finfo->size < 1 || finfo->start == 0x7FFFFFFF)
  980. return -EINVAL;
  981. if (finfo->frag) {
  982. datablk_count = finfo->size / le32_to_cpu(blksz);
  983. ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment),
  984. fentry);
  985. if (ret < 0)
  986. return -EINVAL;
  987. finfo->comp = true;
  988. if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
  989. return -EINVAL;
  990. } else {
  991. datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
  992. }
  993. finfo->blk_sizes = malloc(datablk_count * sizeof(u32));
  994. if (!finfo->blk_sizes)
  995. return -ENOMEM;
  996. return datablk_count;
  997. }
  998. int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
  999. loff_t *actread)
  1000. {
  1001. char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL;
  1002. char *fragment, *file, *resolved, *data;
  1003. u64 start, n_blks, table_size, data_offset, table_offset;
  1004. int ret, j, i_number, datablk_count = 0;
  1005. struct squashfs_super_block *sblk = ctxt.sblk;
  1006. struct squashfs_fragment_block_entry frag_entry;
  1007. struct squashfs_file_info finfo = {0};
  1008. struct squashfs_symlink_inode *symlink;
  1009. struct fs_dir_stream *dirsp = NULL;
  1010. struct squashfs_dir_stream *dirs;
  1011. struct squashfs_lreg_inode *lreg;
  1012. struct squashfs_base_inode *base;
  1013. struct squashfs_reg_inode *reg;
  1014. unsigned long dest_len;
  1015. struct fs_dirent *dent;
  1016. unsigned char *ipos;
  1017. *actread = 0;
  1018. /*
  1019. * sqfs_opendir will uncompress inode and directory tables, and will
  1020. * return a pointer to the directory that contains the requested file.
  1021. */
  1022. sqfs_split_path(&file, &dir, filename);
  1023. ret = sqfs_opendir(dir, &dirsp);
  1024. if (ret) {
  1025. sqfs_closedir(dirsp);
  1026. goto free_paths;
  1027. }
  1028. dirs = (struct squashfs_dir_stream *)dirsp;
  1029. /* For now, only regular files are able to be loaded */
  1030. while (!sqfs_readdir(dirsp, &dent)) {
  1031. ret = strcmp(dent->name, file);
  1032. if (!ret)
  1033. break;
  1034. free(dirs->entry);
  1035. }
  1036. if (ret) {
  1037. printf("File not found.\n");
  1038. *actread = 0;
  1039. sqfs_closedir(dirsp);
  1040. ret = -ENOENT;
  1041. goto free_paths;
  1042. }
  1043. i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
  1044. ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
  1045. sblk->block_size);
  1046. base = (struct squashfs_base_inode *)ipos;
  1047. switch (get_unaligned_le16(&base->inode_type)) {
  1048. case SQFS_REG_TYPE:
  1049. reg = (struct squashfs_reg_inode *)ipos;
  1050. datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry,
  1051. sblk->block_size);
  1052. if (datablk_count < 0) {
  1053. ret = -EINVAL;
  1054. goto free_paths;
  1055. }
  1056. memcpy(finfo.blk_sizes, ipos + sizeof(*reg),
  1057. datablk_count * sizeof(u32));
  1058. break;
  1059. case SQFS_LREG_TYPE:
  1060. lreg = (struct squashfs_lreg_inode *)ipos;
  1061. datablk_count = sqfs_get_lregfile_info(lreg, &finfo,
  1062. &frag_entry,
  1063. sblk->block_size);
  1064. if (datablk_count < 0) {
  1065. ret = -EINVAL;
  1066. goto free_paths;
  1067. }
  1068. memcpy(finfo.blk_sizes, ipos + sizeof(*lreg),
  1069. datablk_count * sizeof(u32));
  1070. break;
  1071. case SQFS_SYMLINK_TYPE:
  1072. case SQFS_LSYMLINK_TYPE:
  1073. symlink = (struct squashfs_symlink_inode *)ipos;
  1074. resolved = sqfs_resolve_symlink(symlink, filename);
  1075. ret = sqfs_read(resolved, buf, offset, len, actread);
  1076. free(resolved);
  1077. goto free_paths;
  1078. case SQFS_BLKDEV_TYPE:
  1079. case SQFS_CHRDEV_TYPE:
  1080. case SQFS_LBLKDEV_TYPE:
  1081. case SQFS_LCHRDEV_TYPE:
  1082. case SQFS_FIFO_TYPE:
  1083. case SQFS_SOCKET_TYPE:
  1084. case SQFS_LFIFO_TYPE:
  1085. case SQFS_LSOCKET_TYPE:
  1086. default:
  1087. printf("Unsupported entry type\n");
  1088. ret = -EINVAL;
  1089. goto free_paths;
  1090. }
  1091. /* If the user specifies a length, check its sanity */
  1092. if (len) {
  1093. if (len > finfo.size) {
  1094. ret = -EINVAL;
  1095. goto free_paths;
  1096. }
  1097. finfo.size = len;
  1098. }
  1099. if (datablk_count) {
  1100. data_offset = finfo.start;
  1101. datablock = malloc(get_unaligned_le32(&sblk->block_size));
  1102. if (!datablock) {
  1103. ret = -ENOMEM;
  1104. goto free_paths;
  1105. }
  1106. }
  1107. for (j = 0; j < datablk_count; j++) {
  1108. start = data_offset / ctxt.cur_dev->blksz;
  1109. table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
  1110. table_offset = data_offset - (start * ctxt.cur_dev->blksz);
  1111. n_blks = DIV_ROUND_UP(table_size + table_offset,
  1112. ctxt.cur_dev->blksz);
  1113. data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  1114. if (!data_buffer) {
  1115. ret = -ENOMEM;
  1116. goto free_datablk;
  1117. }
  1118. ret = sqfs_disk_read(start, n_blks, data_buffer);
  1119. if (ret < 0) {
  1120. /*
  1121. * Possible causes: too many data blocks or too large
  1122. * SquashFS block size. Tip: re-compile the SquashFS
  1123. * image with mksquashfs's -b <block_size> option.
  1124. */
  1125. printf("Error: too many data blocks to be read.\n");
  1126. goto free_buffer;
  1127. }
  1128. data = data_buffer + table_offset;
  1129. /* Load the data */
  1130. if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
  1131. dest_len = get_unaligned_le32(&sblk->block_size);
  1132. ret = sqfs_decompress(&ctxt, datablock, &dest_len,
  1133. data, table_size);
  1134. if (ret)
  1135. goto free_buffer;
  1136. memcpy(buf + offset + *actread, datablock, dest_len);
  1137. *actread += dest_len;
  1138. } else {
  1139. memcpy(buf + offset + *actread, data, table_size);
  1140. *actread += table_size;
  1141. }
  1142. data_offset += table_size;
  1143. }
  1144. free(finfo.blk_sizes);
  1145. /*
  1146. * There is no need to continue if the file is not fragmented.
  1147. */
  1148. if (!finfo.frag) {
  1149. ret = 0;
  1150. goto free_buffer;
  1151. }
  1152. start = frag_entry.start / ctxt.cur_dev->blksz;
  1153. table_size = SQFS_BLOCK_SIZE(frag_entry.size);
  1154. table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
  1155. n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
  1156. fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz);
  1157. if (!fragment) {
  1158. ret = -ENOMEM;
  1159. goto free_buffer;
  1160. }
  1161. ret = sqfs_disk_read(start, n_blks, fragment);
  1162. if (ret < 0)
  1163. goto free_fragment;
  1164. /* File compressed and fragmented */
  1165. if (finfo.frag && finfo.comp) {
  1166. dest_len = get_unaligned_le32(&sblk->block_size);
  1167. fragment_block = malloc(dest_len);
  1168. if (!fragment_block) {
  1169. ret = -ENOMEM;
  1170. goto free_fragment;
  1171. }
  1172. ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
  1173. (void *)fragment + table_offset,
  1174. frag_entry.size);
  1175. if (ret) {
  1176. free(fragment_block);
  1177. goto free_fragment;
  1178. }
  1179. for (j = offset + *actread; j < finfo.size; j++) {
  1180. memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
  1181. (*actread)++;
  1182. }
  1183. free(fragment_block);
  1184. } else if (finfo.frag && !finfo.comp) {
  1185. fragment_block = (void *)fragment + table_offset;
  1186. for (j = offset + *actread; j < finfo.size; j++) {
  1187. memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
  1188. (*actread)++;
  1189. }
  1190. }
  1191. free_fragment:
  1192. free(fragment);
  1193. free_buffer:
  1194. if (datablk_count)
  1195. free(data_buffer);
  1196. free_datablk:
  1197. if (datablk_count)
  1198. free(datablock);
  1199. free_paths:
  1200. free(file);
  1201. free(dir);
  1202. return ret;
  1203. }
  1204. int sqfs_size(const char *filename, loff_t *size)
  1205. {
  1206. struct squashfs_super_block *sblk = ctxt.sblk;
  1207. struct squashfs_symlink_inode *symlink;
  1208. struct fs_dir_stream *dirsp = NULL;
  1209. struct squashfs_base_inode *base;
  1210. struct squashfs_dir_stream *dirs;
  1211. struct squashfs_lreg_inode *lreg;
  1212. struct squashfs_reg_inode *reg;
  1213. char *dir, *file, *resolved;
  1214. struct fs_dirent *dent;
  1215. unsigned char *ipos;
  1216. int ret, i_number;
  1217. sqfs_split_path(&file, &dir, filename);
  1218. /*
  1219. * sqfs_opendir will uncompress inode and directory tables, and will
  1220. * return a pointer to the directory that contains the requested file.
  1221. */
  1222. ret = sqfs_opendir(dir, &dirsp);
  1223. if (ret) {
  1224. sqfs_closedir(dirsp);
  1225. ret = -EINVAL;
  1226. goto free_strings;
  1227. }
  1228. dirs = (struct squashfs_dir_stream *)dirsp;
  1229. while (!sqfs_readdir(dirsp, &dent)) {
  1230. ret = strcmp(dent->name, file);
  1231. if (!ret)
  1232. break;
  1233. free(dirs->entry);
  1234. }
  1235. if (ret) {
  1236. printf("File not found.\n");
  1237. *size = 0;
  1238. ret = -EINVAL;
  1239. goto free_strings;
  1240. }
  1241. i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
  1242. ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes,
  1243. sblk->block_size);
  1244. free(dirs->entry);
  1245. base = (struct squashfs_base_inode *)ipos;
  1246. switch (get_unaligned_le16(&base->inode_type)) {
  1247. case SQFS_REG_TYPE:
  1248. reg = (struct squashfs_reg_inode *)ipos;
  1249. *size = get_unaligned_le32(&reg->file_size);
  1250. break;
  1251. case SQFS_LREG_TYPE:
  1252. lreg = (struct squashfs_lreg_inode *)ipos;
  1253. *size = get_unaligned_le64(&lreg->file_size);
  1254. break;
  1255. case SQFS_SYMLINK_TYPE:
  1256. case SQFS_LSYMLINK_TYPE:
  1257. symlink = (struct squashfs_symlink_inode *)ipos;
  1258. resolved = sqfs_resolve_symlink(symlink, filename);
  1259. ret = sqfs_size(resolved, size);
  1260. free(resolved);
  1261. break;
  1262. case SQFS_BLKDEV_TYPE:
  1263. case SQFS_CHRDEV_TYPE:
  1264. case SQFS_LBLKDEV_TYPE:
  1265. case SQFS_LCHRDEV_TYPE:
  1266. case SQFS_FIFO_TYPE:
  1267. case SQFS_SOCKET_TYPE:
  1268. case SQFS_LFIFO_TYPE:
  1269. case SQFS_LSOCKET_TYPE:
  1270. default:
  1271. printf("Unable to recover entry's size.\n");
  1272. *size = 0;
  1273. ret = -EINVAL;
  1274. break;
  1275. }
  1276. free_strings:
  1277. free(dir);
  1278. free(file);
  1279. sqfs_closedir(dirsp);
  1280. return ret;
  1281. }
  1282. void sqfs_close(void)
  1283. {
  1284. free(ctxt.sblk);
  1285. ctxt.cur_dev = NULL;
  1286. sqfs_decompressor_cleanup(&ctxt);
  1287. }
  1288. void sqfs_closedir(struct fs_dir_stream *dirs)
  1289. {
  1290. struct squashfs_dir_stream *sqfs_dirs;
  1291. sqfs_dirs = (struct squashfs_dir_stream *)dirs;
  1292. free(sqfs_dirs->inode_table);
  1293. free(sqfs_dirs->dir_table);
  1294. free(sqfs_dirs->dir_header);
  1295. }