2010-12-08 20:22:46 +00:00
|
|
|
/* squash4.c - SquashFS */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2010 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/fshelp.h>
|
|
|
|
#include <grub/deflate.h>
|
2020-06-15 16:28:27 +00:00
|
|
|
#include <grub/safemath.h>
|
2011-12-26 12:18:01 +00:00
|
|
|
#include <minilzo.h>
|
|
|
|
|
|
|
|
#include "xz.h"
|
|
|
|
#include "xz_stream.h"
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-04-21 09:37:45 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
/*
|
2010-12-09 16:06:49 +00:00
|
|
|
object format Pointed by
|
|
|
|
superblock RAW Fixed offset (0)
|
|
|
|
data RAW ? Fixed offset (60)
|
|
|
|
inode table Chunk superblock
|
|
|
|
dir table Chunk superblock
|
|
|
|
fragment table Chunk unk1
|
|
|
|
unk1 RAW, Chunk superblock
|
|
|
|
unk2 RAW superblock
|
|
|
|
UID/GID Chunk exttblptr
|
|
|
|
exttblptr RAW superblock
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
UID/GID table is the array ot uint32_t
|
2011-10-25 16:09:00 +00:00
|
|
|
unk1 contains pointer to fragment table followed by some chunk.
|
2010-12-08 20:22:46 +00:00
|
|
|
unk2 containts one uint64_t
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct grub_squash_super
|
|
|
|
{
|
|
|
|
grub_uint32_t magic;
|
|
|
|
#define SQUASH_MAGIC 0x73717368
|
|
|
|
grub_uint32_t dummy1;
|
|
|
|
grub_uint32_t creation_time;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint32_t block_size;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy2;
|
|
|
|
grub_uint16_t compression;
|
|
|
|
grub_uint16_t dummy3;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint64_t dummy4;
|
2010-12-09 01:48:59 +00:00
|
|
|
grub_uint16_t root_ino_offset;
|
2010-12-11 17:41:42 +00:00
|
|
|
grub_uint32_t root_ino_chunk;
|
|
|
|
grub_uint16_t dummy5;
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_uint64_t total_size;
|
|
|
|
grub_uint64_t exttbloffset;
|
2010-12-11 17:41:42 +00:00
|
|
|
grub_uint64_t dummy6;
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_uint64_t inodeoffset;
|
|
|
|
grub_uint64_t diroffset;
|
|
|
|
grub_uint64_t unk1offset;
|
|
|
|
grub_uint64_t unk2offset;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
/* Chunk-based */
|
|
|
|
struct grub_squash_inode
|
|
|
|
{
|
|
|
|
/* Same values as direlem types. */
|
|
|
|
grub_uint16_t type;
|
2010-12-09 20:14:42 +00:00
|
|
|
grub_uint16_t dummy[3];
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_uint32_t mtime;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy2;
|
2010-12-09 20:14:42 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
grub_uint32_t chunk;
|
|
|
|
grub_uint32_t fragment;
|
2012-05-04 15:13:24 +00:00
|
|
|
grub_uint32_t offset;
|
2010-12-09 20:14:42 +00:00
|
|
|
grub_uint32_t size;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint32_t block_size[0];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED file;
|
2011-10-25 16:09:00 +00:00
|
|
|
struct {
|
|
|
|
grub_uint64_t chunk;
|
|
|
|
grub_uint64_t size;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy1[3];
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint32_t fragment;
|
2012-05-04 15:13:24 +00:00
|
|
|
grub_uint32_t offset;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy3;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint32_t block_size[0];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED long_file;
|
2010-12-09 20:14:42 +00:00
|
|
|
struct {
|
|
|
|
grub_uint32_t chunk;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy;
|
2010-12-09 20:14:42 +00:00
|
|
|
grub_uint16_t size;
|
2011-12-13 22:14:25 +00:00
|
|
|
grub_uint16_t offset;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED dir;
|
2010-12-09 21:29:36 +00:00
|
|
|
struct {
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t dummy1;
|
|
|
|
grub_uint32_t size;
|
|
|
|
grub_uint32_t chunk;
|
|
|
|
grub_uint32_t dummy2;
|
|
|
|
grub_uint16_t dummy3;
|
|
|
|
grub_uint16_t offset;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED long_dir;
|
2011-12-26 12:18:01 +00:00
|
|
|
struct {
|
|
|
|
grub_uint32_t dummy;
|
2010-12-09 21:29:36 +00:00
|
|
|
grub_uint32_t namelen;
|
|
|
|
char name[0];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED symlink;
|
|
|
|
} GRUB_PACKED;
|
|
|
|
} GRUB_PACKED;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
struct grub_squash_cache_inode
|
|
|
|
{
|
|
|
|
struct grub_squash_inode ino;
|
|
|
|
grub_disk_addr_t ino_chunk;
|
|
|
|
grub_uint16_t ino_offset;
|
|
|
|
grub_uint32_t *block_sizes;
|
|
|
|
grub_disk_addr_t *cumulated_block_sizes;
|
|
|
|
};
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
/* Chunk-based. */
|
|
|
|
struct grub_squash_dirent_header
|
|
|
|
{
|
|
|
|
/* Actually the value is the number of elements - 1. */
|
2010-12-11 17:32:08 +00:00
|
|
|
grub_uint32_t nelems;
|
2011-12-13 22:14:25 +00:00
|
|
|
grub_uint32_t ino_chunk;
|
|
|
|
grub_uint32_t dummy;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
struct grub_squash_dirent
|
|
|
|
{
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_uint16_t ino_offset;
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_uint16_t dummy;
|
|
|
|
grub_uint16_t type;
|
|
|
|
/* Actually the value is the length of name - 1. */
|
|
|
|
grub_uint16_t namelen;
|
|
|
|
char name[0];
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SQUASH_TYPE_DIR = 1,
|
|
|
|
SQUASH_TYPE_REGULAR = 2,
|
|
|
|
SQUASH_TYPE_SYMLINK = 3,
|
2011-12-26 12:18:01 +00:00
|
|
|
SQUASH_TYPE_LONG_DIR = 8,
|
2011-10-25 16:09:00 +00:00
|
|
|
SQUASH_TYPE_LONG_REGULAR = 9,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-09 16:06:49 +00:00
|
|
|
struct grub_squash_frag_desc
|
|
|
|
{
|
|
|
|
grub_uint64_t offset;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_uint32_t size;
|
|
|
|
grub_uint32_t dummy;
|
2013-12-15 13:14:30 +00:00
|
|
|
} GRUB_PACKED;
|
2010-12-09 16:06:49 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SQUASH_CHUNK_FLAGS = 0x8000,
|
|
|
|
SQUASH_CHUNK_UNCOMPRESSED = 0x8000
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SQUASH_BLOCK_FLAGS = 0x1000000,
|
|
|
|
SQUASH_BLOCK_UNCOMPRESSED = 0x1000000
|
|
|
|
};
|
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
COMPRESSION_ZLIB = 1,
|
|
|
|
COMPRESSION_LZO = 3,
|
|
|
|
COMPRESSION_XZ = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
#define SQUASH_CHUNK_SIZE 0x2000
|
2011-12-26 12:18:01 +00:00
|
|
|
#define XZBUFSIZ 0x2000
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2010-12-09 16:06:49 +00:00
|
|
|
struct grub_squash_data
|
|
|
|
{
|
|
|
|
grub_disk_t disk;
|
|
|
|
struct grub_squash_super sb;
|
2011-10-25 16:09:00 +00:00
|
|
|
struct grub_squash_cache_inode ino;
|
2010-12-09 16:06:49 +00:00
|
|
|
grub_uint64_t fragments;
|
2011-10-28 14:05:16 +00:00
|
|
|
int log2_blksz;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_size_t blksz;
|
|
|
|
grub_ssize_t (*decompress) (char *inbuf, grub_size_t insize, grub_off_t off,
|
|
|
|
char *outbuf, grub_size_t outsize,
|
|
|
|
struct grub_squash_data *data);
|
|
|
|
struct xz_dec *xzdec;
|
|
|
|
char *xzbuf;
|
2010-12-09 16:06:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct grub_fshelp_node
|
|
|
|
{
|
|
|
|
struct grub_squash_data *data;
|
|
|
|
struct grub_squash_inode ino;
|
2012-05-27 11:44:48 +00:00
|
|
|
grub_size_t stsize;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
grub_disk_addr_t ino_chunk;
|
|
|
|
grub_uint16_t ino_offset;
|
|
|
|
} stack[1];
|
2010-12-09 16:06:49 +00:00
|
|
|
};
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static grub_err_t
|
2011-10-25 16:09:00 +00:00
|
|
|
read_chunk (struct grub_squash_data *data, void *buf, grub_size_t len,
|
2011-12-13 22:14:25 +00:00
|
|
|
grub_uint64_t chunk_start, grub_off_t offset)
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
grub_uint64_t csize;
|
|
|
|
grub_uint16_t d;
|
|
|
|
grub_err_t err;
|
|
|
|
while (1)
|
|
|
|
{
|
2011-10-25 16:09:00 +00:00
|
|
|
err = grub_disk_read (data->disk,
|
|
|
|
chunk_start >> GRUB_DISK_SECTOR_BITS,
|
2010-12-08 20:22:46 +00:00
|
|
|
chunk_start & (GRUB_DISK_SECTOR_SIZE - 1),
|
|
|
|
sizeof (d), &d);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2010-12-09 18:23:35 +00:00
|
|
|
if (offset < SQUASH_CHUNK_SIZE)
|
2010-12-08 20:22:46 +00:00
|
|
|
break;
|
2010-12-09 18:23:35 +00:00
|
|
|
offset -= SQUASH_CHUNK_SIZE;
|
2010-12-08 20:22:46 +00:00
|
|
|
chunk_start += 2 + (grub_le_to_cpu16 (d) & ~SQUASH_CHUNK_FLAGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
csize = SQUASH_CHUNK_SIZE - offset;
|
|
|
|
if (csize > len)
|
|
|
|
csize = len;
|
|
|
|
|
|
|
|
if (grub_le_to_cpu16 (d) & SQUASH_CHUNK_UNCOMPRESSED)
|
|
|
|
{
|
|
|
|
grub_disk_addr_t a = chunk_start + 2 + offset;
|
2011-10-25 16:09:00 +00:00
|
|
|
err = grub_disk_read (data->disk, (a >> GRUB_DISK_SECTOR_BITS),
|
2010-12-08 20:22:46 +00:00
|
|
|
a & (GRUB_DISK_SECTOR_SIZE - 1),
|
|
|
|
csize, buf);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-09 18:23:35 +00:00
|
|
|
char *tmp;
|
|
|
|
grub_size_t bsize = grub_le_to_cpu16 (d) & ~SQUASH_CHUNK_FLAGS;
|
|
|
|
grub_disk_addr_t a = chunk_start + 2;
|
|
|
|
tmp = grub_malloc (bsize);
|
|
|
|
if (!tmp)
|
|
|
|
return grub_errno;
|
|
|
|
/* FIXME: buffer uncompressed data. */
|
2011-10-25 16:09:00 +00:00
|
|
|
err = grub_disk_read (data->disk, (a >> GRUB_DISK_SECTOR_BITS),
|
2010-12-09 18:23:35 +00:00
|
|
|
a & (GRUB_DISK_SECTOR_SIZE - 1),
|
|
|
|
bsize, tmp);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (tmp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
if (data->decompress (tmp, bsize, offset,
|
|
|
|
buf, csize, data) < 0)
|
2010-12-09 18:23:35 +00:00
|
|
|
{
|
|
|
|
grub_free (tmp);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
grub_free (tmp);
|
2010-12-08 20:22:46 +00:00
|
|
|
}
|
|
|
|
len -= csize;
|
|
|
|
offset += csize;
|
|
|
|
buf = (char *) buf + csize;
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
static grub_ssize_t
|
|
|
|
zlib_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
|
|
|
|
char *outbuf, grub_size_t outsize,
|
|
|
|
struct grub_squash_data *data __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
return grub_zlib_decompress (inbuf, insize, off, outbuf, outsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_ssize_t
|
|
|
|
lzo_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
|
|
|
|
char *outbuf, grub_size_t len, struct grub_squash_data *data)
|
|
|
|
{
|
|
|
|
lzo_uint usize = data->blksz;
|
|
|
|
grub_uint8_t *udata;
|
|
|
|
|
2012-04-25 23:16:25 +00:00
|
|
|
if (usize < 8192)
|
|
|
|
usize = 8192;
|
|
|
|
|
|
|
|
udata = grub_malloc (usize);
|
2011-12-26 12:18:01 +00:00
|
|
|
if (!udata)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (lzo1x_decompress_safe ((grub_uint8_t *) inbuf,
|
|
|
|
insize, udata, &usize, NULL) != LZO_E_OK)
|
|
|
|
{
|
2012-04-25 23:16:25 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FS, "incorrect compressed chunk");
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_free (udata);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
grub_memcpy (outbuf, udata + off, len);
|
|
|
|
grub_free (udata);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_ssize_t
|
|
|
|
xz_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
|
|
|
|
char *outbuf, grub_size_t len, struct grub_squash_data *data)
|
|
|
|
{
|
2012-01-27 12:09:57 +00:00
|
|
|
grub_size_t ret = 0;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_off_t pos = 0;
|
|
|
|
struct xz_buf buf;
|
|
|
|
|
|
|
|
xz_dec_reset (data->xzdec);
|
|
|
|
buf.in = (grub_uint8_t *) inbuf;
|
|
|
|
buf.in_pos = 0;
|
|
|
|
buf.in_size = insize;
|
|
|
|
buf.out = (grub_uint8_t *) data->xzbuf;
|
|
|
|
buf.out_pos = 0;
|
|
|
|
buf.out_size = XZBUFSIZ;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
enum xz_ret xzret;
|
|
|
|
|
|
|
|
buf.out_pos = 0;
|
|
|
|
|
|
|
|
xzret = xz_dec_run (data->xzdec, &buf);
|
|
|
|
|
|
|
|
if (xzret != XZ_OK && xzret != XZ_STREAM_END)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "invalid xz chunk");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pos + buf.out_pos >= off)
|
|
|
|
{
|
|
|
|
grub_ssize_t outoff = pos - off;
|
|
|
|
grub_size_t l;
|
|
|
|
if (outoff >= 0)
|
|
|
|
{
|
|
|
|
l = buf.out_pos;
|
|
|
|
if (l > len)
|
|
|
|
l = len;
|
|
|
|
grub_memcpy (outbuf + outoff, buf.out, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outoff = -outoff;
|
|
|
|
l = buf.out_pos - outoff;
|
|
|
|
if (l > len)
|
|
|
|
l = len;
|
|
|
|
grub_memcpy (outbuf, buf.out + outoff, l);
|
|
|
|
}
|
|
|
|
ret += l;
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
pos += buf.out_pos;
|
|
|
|
if (xzret == XZ_STREAM_END)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static struct grub_squash_data *
|
|
|
|
squash_mount (grub_disk_t disk)
|
|
|
|
{
|
|
|
|
struct grub_squash_super sb;
|
|
|
|
grub_err_t err;
|
|
|
|
struct grub_squash_data *data;
|
2010-12-09 16:06:49 +00:00
|
|
|
grub_uint64_t frag;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
err = grub_disk_read (disk, 0, 0, sizeof (sb), &sb);
|
|
|
|
if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "not a squash4");
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
2011-12-26 12:18:01 +00:00
|
|
|
if (sb.magic != grub_cpu_to_le32_compile_time (SQUASH_MAGIC)
|
|
|
|
|| sb.block_size == 0
|
|
|
|
|| ((sb.block_size - 1) & sb.block_size))
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "not squash4");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-09 16:06:49 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
err = grub_disk_read (disk,
|
|
|
|
grub_le_to_cpu64 (sb.unk1offset)
|
2010-12-09 16:06:49 +00:00
|
|
|
>> GRUB_DISK_SECTOR_BITS,
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_le_to_cpu64 (sb.unk1offset)
|
2010-12-09 16:06:49 +00:00
|
|
|
& (GRUB_DISK_SECTOR_SIZE - 1), sizeof (frag), &frag);
|
|
|
|
if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "not a squash4");
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-09 14:01:58 +00:00
|
|
|
data = grub_zalloc (sizeof (*data));
|
2010-12-08 20:22:46 +00:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
data->sb = sb;
|
|
|
|
data->disk = disk;
|
2011-10-25 16:09:00 +00:00
|
|
|
data->fragments = grub_le_to_cpu64 (frag);
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (sb.compression)
|
|
|
|
{
|
2012-01-11 11:46:50 +00:00
|
|
|
case grub_cpu_to_le16_compile_time (COMPRESSION_ZLIB):
|
2011-12-26 12:18:01 +00:00
|
|
|
data->decompress = zlib_decompress;
|
|
|
|
break;
|
2012-01-11 11:46:50 +00:00
|
|
|
case grub_cpu_to_le16_compile_time (COMPRESSION_LZO):
|
2011-12-26 12:18:01 +00:00
|
|
|
data->decompress = lzo_decompress;
|
|
|
|
break;
|
2012-01-11 11:46:50 +00:00
|
|
|
case grub_cpu_to_le16_compile_time (COMPRESSION_XZ):
|
2011-12-26 12:18:01 +00:00
|
|
|
data->decompress = xz_decompress;
|
|
|
|
data->xzbuf = grub_malloc (XZBUFSIZ);
|
|
|
|
if (!data->xzbuf)
|
|
|
|
{
|
|
|
|
grub_free (data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data->xzdec = xz_dec_init (1 << 16);
|
|
|
|
if (!data->xzdec)
|
|
|
|
{
|
|
|
|
grub_free (data->xzbuf);
|
|
|
|
grub_free (data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
grub_free (data);
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "unsupported compression %d",
|
|
|
|
grub_le_to_cpu16 (sb.compression));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->blksz = grub_le_to_cpu32 (data->sb.block_size);
|
2011-10-28 14:05:16 +00:00
|
|
|
for (data->log2_blksz = 0;
|
2011-12-26 12:18:01 +00:00
|
|
|
(1U << data->log2_blksz) < data->blksz;
|
2011-10-28 14:05:16 +00:00
|
|
|
data->log2_blksz++);
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2010-12-09 21:29:36 +00:00
|
|
|
static char *
|
|
|
|
grub_squash_read_symlink (grub_fshelp_node_t node)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
grub_err_t err;
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_size_t sz;
|
|
|
|
|
|
|
|
if (grub_add (grub_le_to_cpu32 (node->ino.symlink.namelen), 1, &sz))
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = grub_malloc (sz);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
2010-12-09 21:29:36 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
err = read_chunk (node->data, ret,
|
2010-12-09 21:29:36 +00:00
|
|
|
grub_le_to_cpu32 (node->ino.symlink.namelen),
|
|
|
|
grub_le_to_cpu64 (node->data->sb.inodeoffset)
|
2012-05-27 11:44:48 +00:00
|
|
|
+ node->stack[node->stsize - 1].ino_chunk,
|
|
|
|
node->stack[node->stsize - 1].ino_offset
|
|
|
|
+ (node->ino.symlink.name - (char *) &node->ino));
|
2010-12-09 21:29:36 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret[grub_le_to_cpu32 (node->ino.symlink.namelen)] = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static int
|
|
|
|
grub_squash_iterate_dir (grub_fshelp_node_t dir,
|
2013-01-21 01:33:46 +00:00
|
|
|
grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint32_t off;
|
2010-12-09 17:22:38 +00:00
|
|
|
grub_uint32_t endoff;
|
2011-12-26 12:18:01 +00:00
|
|
|
grub_uint64_t chunk;
|
2010-12-09 17:22:38 +00:00
|
|
|
unsigned i;
|
2010-12-09 01:48:59 +00:00
|
|
|
|
2010-12-09 17:22:38 +00:00
|
|
|
/* FIXME: why - 3 ? */
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (dir->ino.type)
|
|
|
|
{
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_DIR):
|
|
|
|
off = grub_le_to_cpu16 (dir->ino.dir.offset);
|
|
|
|
endoff = grub_le_to_cpu16 (dir->ino.dir.size) + off - 3;
|
|
|
|
chunk = grub_le_to_cpu32 (dir->ino.dir.chunk);
|
|
|
|
break;
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_DIR):
|
|
|
|
off = grub_le_to_cpu16 (dir->ino.long_dir.offset);
|
|
|
|
endoff = grub_le_to_cpu16 (dir->ino.long_dir.size) + off - 3;
|
|
|
|
chunk = grub_le_to_cpu32 (dir->ino.long_dir.chunk);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "unexpected ino type 0x%x",
|
|
|
|
grub_le_to_cpu16 (dir->ino.type));
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-09 17:22:38 +00:00
|
|
|
|
2012-05-27 11:44:48 +00:00
|
|
|
{
|
|
|
|
grub_fshelp_node_t node;
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_size_t sz;
|
|
|
|
|
|
|
|
if (grub_mul (dir->stsize, sizeof (dir->stack[0]), &sz) ||
|
|
|
|
grub_add (sz, sizeof (*node), &sz))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
node = grub_malloc (sz);
|
2012-05-27 11:44:48 +00:00
|
|
|
if (!node)
|
|
|
|
return 0;
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_memcpy (node, dir, sz);
|
2013-01-21 01:33:46 +00:00
|
|
|
if (hook (".", GRUB_FSHELP_DIR, node, hook_data))
|
2012-05-27 11:44:48 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (dir->stsize != 1)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
if (grub_mul (dir->stsize, sizeof (dir->stack[0]), &sz) ||
|
|
|
|
grub_add (sz, sizeof (*node), &sz))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
node = grub_malloc (sz);
|
2012-05-27 11:44:48 +00:00
|
|
|
if (!node)
|
|
|
|
return 0;
|
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_memcpy (node, dir, sz);
|
2012-05-27 11:44:48 +00:00
|
|
|
|
|
|
|
node->stsize--;
|
|
|
|
err = read_chunk (dir->data, &node->ino, sizeof (node->ino),
|
|
|
|
grub_le_to_cpu64 (dir->data->sb.inodeoffset)
|
|
|
|
+ node->stack[node->stsize - 1].ino_chunk,
|
|
|
|
node->stack[node->stsize - 1].ino_offset);
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
if (hook ("..", GRUB_FSHELP_DIR, node, hook_data))
|
2012-05-27 11:44:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-09 17:22:38 +00:00
|
|
|
while (off < endoff)
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
|
|
|
struct grub_squash_dirent_header dh;
|
|
|
|
grub_err_t err;
|
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
err = read_chunk (dir->data, &dh, sizeof (dh),
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_le_to_cpu64 (dir->data->sb.diroffset)
|
2011-12-26 12:18:01 +00:00
|
|
|
+ chunk, off);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
off += sizeof (dh);
|
2011-12-13 22:14:25 +00:00
|
|
|
for (i = 0; i < (unsigned) grub_le_to_cpu32 (dh.nelems) + 1; i++)
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int r;
|
|
|
|
struct grub_fshelp_node *node;
|
|
|
|
enum grub_fshelp_filetype filetype = GRUB_FSHELP_REG;
|
|
|
|
struct grub_squash_dirent di;
|
|
|
|
struct grub_squash_inode ino;
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_size_t sz;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
err = read_chunk (dir->data, &di, sizeof (di),
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_le_to_cpu64 (dir->data->sb.diroffset)
|
2011-12-26 12:18:01 +00:00
|
|
|
+ chunk, off);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
off += sizeof (di);
|
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
err = read_chunk (dir->data, &ino, sizeof (ino),
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_le_to_cpu64 (dir->data->sb.inodeoffset)
|
|
|
|
+ grub_le_to_cpu32 (dh.ino_chunk),
|
|
|
|
grub_cpu_to_le16 (di.ino_offset));
|
2010-12-08 20:22:46 +00:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
buf = grub_malloc (grub_le_to_cpu16 (di.namelen) + 2);
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
2011-10-25 16:09:00 +00:00
|
|
|
err = read_chunk (dir->data, buf,
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_le_to_cpu16 (di.namelen) + 1,
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_le_to_cpu64 (dir->data->sb.diroffset)
|
2011-12-26 12:18:01 +00:00
|
|
|
+ chunk, off);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
off += grub_le_to_cpu16 (di.namelen) + 1;
|
|
|
|
buf[grub_le_to_cpu16 (di.namelen) + 1] = 0;
|
2010-12-09 18:23:35 +00:00
|
|
|
if (grub_le_to_cpu16 (di.type) == SQUASH_TYPE_DIR)
|
2010-12-08 20:22:46 +00:00
|
|
|
filetype = GRUB_FSHELP_DIR;
|
2010-12-09 21:29:36 +00:00
|
|
|
if (grub_le_to_cpu16 (di.type) == SQUASH_TYPE_SYMLINK)
|
|
|
|
filetype = GRUB_FSHELP_SYMLINK;
|
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
if (grub_add (dir->stsize, 1, &sz) ||
|
|
|
|
grub_mul (sz, sizeof (dir->stack[0]), &sz) ||
|
|
|
|
grub_add (sz, sizeof (*node), &sz))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
node = grub_malloc (sz);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (! node)
|
|
|
|
return 0;
|
2010-12-09 21:29:36 +00:00
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_memcpy (node, dir, sz - sizeof(dir->stack[0]));
|
2012-05-27 11:44:48 +00:00
|
|
|
|
|
|
|
node->ino = ino;
|
|
|
|
node->stack[node->stsize].ino_chunk = grub_le_to_cpu32 (dh.ino_chunk);
|
|
|
|
node->stack[node->stsize].ino_offset = grub_le_to_cpu16 (di.ino_offset);
|
|
|
|
node->stsize++;
|
2013-01-21 01:33:46 +00:00
|
|
|
r = hook (buf, filetype, node, hook_data);
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
grub_free (buf);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
make_root_node (struct grub_squash_data *data, struct grub_fshelp_node *root)
|
|
|
|
{
|
|
|
|
grub_memset (root, 0, sizeof (*root));
|
|
|
|
root->data = data;
|
2012-05-27 11:44:48 +00:00
|
|
|
root->stsize = 1;
|
|
|
|
root->stack[0].ino_chunk = grub_le_to_cpu32 (data->sb.root_ino_chunk);
|
|
|
|
root->stack[0].ino_offset = grub_cpu_to_le16 (data->sb.root_ino_offset);
|
2011-10-25 16:09:00 +00:00
|
|
|
return read_chunk (data, &root->ino, sizeof (root->ino),
|
2010-12-09 18:23:35 +00:00
|
|
|
grub_le_to_cpu64 (data->sb.inodeoffset)
|
2012-05-27 11:44:48 +00:00
|
|
|
+ root->stack[0].ino_chunk,
|
|
|
|
root->stack[0].ino_offset);
|
2010-12-08 20:22:46 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 14:01:58 +00:00
|
|
|
static void
|
|
|
|
squash_unmount (struct grub_squash_data *data)
|
|
|
|
{
|
2011-12-26 12:18:01 +00:00
|
|
|
if (data->xzdec)
|
|
|
|
xz_dec_end (data->xzdec);
|
|
|
|
grub_free (data->xzbuf);
|
2011-11-09 14:01:58 +00:00
|
|
|
grub_free (data->ino.cumulated_block_sizes);
|
|
|
|
grub_free (data->ino.block_sizes);
|
|
|
|
grub_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Context for grub_squash_dir. */
|
|
|
|
struct grub_squash_dir_ctx
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
grub_fs_dir_hook_t hook;
|
|
|
|
void *hook_data;
|
|
|
|
};
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Helper for grub_squash_dir. */
|
|
|
|
static int
|
|
|
|
grub_squash_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
|
|
|
|
grub_fshelp_node_t node, void *data)
|
|
|
|
{
|
|
|
|
struct grub_squash_dir_ctx *ctx = data;
|
|
|
|
struct grub_dirhook_info info;
|
|
|
|
|
|
|
|
grub_memset (&info, 0, sizeof (info));
|
|
|
|
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
|
|
|
|
info.mtimeset = 1;
|
|
|
|
info.mtime = grub_le_to_cpu32 (node->ino.mtime);
|
|
|
|
grub_free (node);
|
|
|
|
return ctx->hook (filename, &info, ctx->hook_data);
|
|
|
|
}
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_squash_dir (grub_device_t device, const char *path,
|
|
|
|
grub_fs_dir_hook_t hook, void *hook_data)
|
|
|
|
{
|
|
|
|
struct grub_squash_dir_ctx ctx = { hook, hook_data };
|
2010-12-08 20:22:46 +00:00
|
|
|
struct grub_squash_data *data = 0;
|
|
|
|
struct grub_fshelp_node *fdiro = 0;
|
|
|
|
struct grub_fshelp_node root;
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
data = squash_mount (device->disk);
|
|
|
|
if (! data)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
err = make_root_node (data, &root);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
grub_fshelp_find_file (path, &root, &fdiro, grub_squash_iterate_dir,
|
2010-12-09 21:29:36 +00:00
|
|
|
grub_squash_read_symlink, GRUB_FSHELP_DIR);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (!grub_errno)
|
2013-01-21 01:33:46 +00:00
|
|
|
grub_squash_iterate_dir (fdiro, grub_squash_dir_iter, &ctx);
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-11-09 14:01:58 +00:00
|
|
|
squash_unmount (data);
|
2010-12-08 20:22:46 +00:00
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_squash_open (struct grub_file *file, const char *name)
|
|
|
|
{
|
|
|
|
struct grub_squash_data *data = 0;
|
|
|
|
struct grub_fshelp_node *fdiro = 0;
|
|
|
|
struct grub_fshelp_node root;
|
|
|
|
grub_err_t err;
|
|
|
|
|
|
|
|
data = squash_mount (file->device->disk);
|
|
|
|
if (! data)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
err = make_root_node (data, &root);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
grub_fshelp_find_file (name, &root, &fdiro, grub_squash_iterate_dir,
|
2010-12-09 21:29:36 +00:00
|
|
|
grub_squash_read_symlink, GRUB_FSHELP_REG);
|
2010-12-08 20:22:46 +00:00
|
|
|
if (grub_errno)
|
|
|
|
{
|
2011-11-09 14:01:58 +00:00
|
|
|
squash_unmount (data);
|
2010-12-08 20:22:46 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
2010-12-08 22:03:59 +00:00
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
file->data = data;
|
2011-10-25 16:09:00 +00:00
|
|
|
data->ino.ino = fdiro->ino;
|
|
|
|
data->ino.block_sizes = NULL;
|
|
|
|
data->ino.cumulated_block_sizes = NULL;
|
2012-05-27 11:44:48 +00:00
|
|
|
data->ino.ino_chunk = fdiro->stack[fdiro->stsize - 1].ino_chunk;
|
|
|
|
data->ino.ino_offset = fdiro->stack[fdiro->stsize - 1].ino_offset;
|
2011-10-25 16:09:00 +00:00
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (fdiro->ino.type)
|
|
|
|
{
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_REGULAR):
|
|
|
|
file->size = grub_le_to_cpu64 (fdiro->ino.long_file.size);
|
|
|
|
break;
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_REGULAR):
|
|
|
|
file->size = grub_le_to_cpu32 (fdiro->ino.file.size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
grub_uint16_t type = grub_le_to_cpu16 (fdiro->ino.type);
|
|
|
|
grub_free (fdiro);
|
|
|
|
squash_unmount (data);
|
|
|
|
return grub_error (GRUB_ERR_BAD_FS, "unexpected ino type 0x%x", type);
|
|
|
|
}
|
|
|
|
}
|
2010-12-09 16:06:49 +00:00
|
|
|
|
2011-11-09 14:01:58 +00:00
|
|
|
grub_free (fdiro);
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2011-10-25 16:09:00 +00:00
|
|
|
static grub_ssize_t
|
|
|
|
direct_read (struct grub_squash_data *data,
|
|
|
|
struct grub_squash_cache_inode *ino,
|
|
|
|
grub_off_t off, char *buf, grub_size_t len)
|
|
|
|
{
|
2020-01-27 20:01:16 +00:00
|
|
|
grub_err_t err = GRUB_ERR_NONE;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_off_t cumulated_uncompressed_size = 0;
|
2012-01-11 11:46:50 +00:00
|
|
|
grub_uint64_t a = 0;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_size_t i;
|
|
|
|
grub_size_t origlen = len;
|
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (ino->ino.type)
|
|
|
|
{
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_REGULAR):
|
|
|
|
a = grub_le_to_cpu64 (ino->ino.long_file.chunk);
|
|
|
|
break;
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_REGULAR):
|
|
|
|
a = grub_le_to_cpu32 (ino->ino.file.chunk);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-25 16:09:00 +00:00
|
|
|
|
|
|
|
if (!ino->block_sizes)
|
|
|
|
{
|
2012-01-11 11:46:50 +00:00
|
|
|
grub_off_t total_size = 0;
|
2011-10-25 16:09:00 +00:00
|
|
|
grub_size_t total_blocks;
|
2012-01-11 11:46:50 +00:00
|
|
|
grub_size_t block_offset = 0;
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (ino->ino.type)
|
2011-10-25 16:09:00 +00:00
|
|
|
{
|
2011-12-26 12:18:01 +00:00
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_REGULAR):
|
2011-10-25 16:09:00 +00:00
|
|
|
total_size = grub_le_to_cpu64 (ino->ino.long_file.size);
|
|
|
|
block_offset = ((char *) &ino->ino.long_file.block_size
|
|
|
|
- (char *) &ino->ino);
|
2011-12-26 12:18:01 +00:00
|
|
|
break;
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_REGULAR):
|
2011-10-25 16:09:00 +00:00
|
|
|
total_size = grub_le_to_cpu32 (ino->ino.file.size);
|
|
|
|
block_offset = ((char *) &ino->ino.file.block_size
|
|
|
|
- (char *) &ino->ino);
|
2011-12-26 12:18:01 +00:00
|
|
|
break;
|
2011-10-25 16:09:00 +00:00
|
|
|
}
|
2011-12-26 12:18:01 +00:00
|
|
|
total_blocks = ((total_size + data->blksz - 1) >> data->log2_blksz);
|
2011-10-25 16:09:00 +00:00
|
|
|
ino->block_sizes = grub_malloc (total_blocks
|
|
|
|
* sizeof (ino->block_sizes[0]));
|
|
|
|
ino->cumulated_block_sizes = grub_malloc (total_blocks
|
|
|
|
* sizeof (ino->cumulated_block_sizes[0]));
|
|
|
|
if (!ino->block_sizes || !ino->cumulated_block_sizes)
|
|
|
|
{
|
|
|
|
grub_free (ino->block_sizes);
|
|
|
|
grub_free (ino->cumulated_block_sizes);
|
|
|
|
ino->block_sizes = 0;
|
|
|
|
ino->cumulated_block_sizes = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = read_chunk (data, ino->block_sizes,
|
|
|
|
total_blocks * sizeof (ino->block_sizes[0]),
|
|
|
|
grub_le_to_cpu64 (data->sb.inodeoffset)
|
|
|
|
+ ino->ino_chunk,
|
|
|
|
ino->ino_offset + block_offset);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (ino->block_sizes);
|
|
|
|
grub_free (ino->cumulated_block_sizes);
|
|
|
|
ino->block_sizes = 0;
|
|
|
|
ino->cumulated_block_sizes = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ino->cumulated_block_sizes[0] = 0;
|
|
|
|
for (i = 1; i < total_blocks; i++)
|
|
|
|
ino->cumulated_block_sizes[i] = ino->cumulated_block_sizes[i - 1]
|
|
|
|
+ (grub_le_to_cpu32 (ino->block_sizes[i - 1]) & ~SQUASH_BLOCK_FLAGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a == 0)
|
|
|
|
a = sizeof (struct grub_squash_super);
|
2011-10-28 14:05:16 +00:00
|
|
|
i = off >> data->log2_blksz;
|
2011-12-26 12:18:01 +00:00
|
|
|
cumulated_uncompressed_size = data->blksz * (grub_disk_addr_t) i;
|
2011-10-25 16:09:00 +00:00
|
|
|
while (cumulated_uncompressed_size < off + len)
|
|
|
|
{
|
2012-02-10 11:17:40 +00:00
|
|
|
grub_size_t boff, curread;
|
2011-10-25 16:09:00 +00:00
|
|
|
boff = off - cumulated_uncompressed_size;
|
2012-02-10 11:17:40 +00:00
|
|
|
curread = data->blksz - boff;
|
|
|
|
if (curread > len)
|
|
|
|
curread = len;
|
2017-02-24 16:10:43 +00:00
|
|
|
if (!ino->block_sizes[i])
|
|
|
|
{
|
|
|
|
/* Sparse block */
|
|
|
|
grub_memset (buf, '\0', curread);
|
|
|
|
}
|
|
|
|
else if (!(ino->block_sizes[i]
|
2011-12-13 22:14:25 +00:00
|
|
|
& grub_cpu_to_le32_compile_time (SQUASH_BLOCK_UNCOMPRESSED)))
|
2011-12-26 12:18:01 +00:00
|
|
|
{
|
|
|
|
char *block;
|
2012-01-27 12:09:57 +00:00
|
|
|
grub_size_t csize;
|
|
|
|
csize = grub_le_to_cpu32 (ino->block_sizes[i]) & ~SQUASH_BLOCK_FLAGS;
|
|
|
|
block = grub_malloc (csize);
|
2011-12-26 12:18:01 +00:00
|
|
|
if (!block)
|
|
|
|
return -1;
|
|
|
|
err = grub_disk_read (data->disk,
|
|
|
|
(ino->cumulated_block_sizes[i] + a)
|
|
|
|
>> GRUB_DISK_SECTOR_BITS,
|
|
|
|
(ino->cumulated_block_sizes[i] + a)
|
|
|
|
& (GRUB_DISK_SECTOR_SIZE - 1),
|
2012-01-27 12:09:57 +00:00
|
|
|
csize, block);
|
2011-12-26 12:18:01 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (block);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-10 11:17:40 +00:00
|
|
|
if (data->decompress (block, csize, boff, buf, curread, data)
|
|
|
|
!= (grub_ssize_t) curread)
|
2011-12-26 12:18:01 +00:00
|
|
|
{
|
|
|
|
grub_free (block);
|
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "incorrect compressed chunk");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
grub_free (block);
|
|
|
|
}
|
2011-10-25 16:09:00 +00:00
|
|
|
else
|
|
|
|
err = grub_disk_read (data->disk,
|
|
|
|
(ino->cumulated_block_sizes[i] + a + boff)
|
|
|
|
>> GRUB_DISK_SECTOR_BITS,
|
|
|
|
(ino->cumulated_block_sizes[i] + a + boff)
|
|
|
|
& (GRUB_DISK_SECTOR_SIZE - 1),
|
2012-02-10 11:17:40 +00:00
|
|
|
curread, buf);
|
2011-10-25 16:09:00 +00:00
|
|
|
if (err)
|
|
|
|
return -1;
|
2012-02-10 11:17:40 +00:00
|
|
|
off += curread;
|
|
|
|
len -= curread;
|
|
|
|
buf += curread;
|
2011-10-25 16:09:00 +00:00
|
|
|
cumulated_uncompressed_size += grub_le_to_cpu32 (data->sb.block_size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return origlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static grub_ssize_t
|
2017-02-24 16:10:43 +00:00
|
|
|
grub_squash_read (grub_file_t file, char *buf, grub_size_t len)
|
2010-12-08 20:22:46 +00:00
|
|
|
{
|
2017-02-24 16:10:43 +00:00
|
|
|
struct grub_squash_data *data = file->data;
|
|
|
|
struct grub_squash_cache_inode *ino = &data->ino;
|
|
|
|
grub_off_t off = file->offset;
|
2010-12-08 20:22:46 +00:00
|
|
|
grub_err_t err;
|
2017-02-24 16:10:43 +00:00
|
|
|
grub_uint64_t a, b;
|
2012-01-11 11:46:50 +00:00
|
|
|
grub_uint32_t fragment = 0;
|
2010-12-09 19:41:41 +00:00
|
|
|
int compressed = 0;
|
2011-10-25 16:09:00 +00:00
|
|
|
struct grub_squash_frag_desc frag;
|
2017-02-24 16:10:43 +00:00
|
|
|
grub_off_t direct_len;
|
|
|
|
grub_uint64_t mask = grub_le_to_cpu32 (data->sb.block_size) - 1;
|
|
|
|
grub_size_t orig_len = len;
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2011-12-26 12:18:01 +00:00
|
|
|
switch (ino->ino.type)
|
2010-12-09 16:06:49 +00:00
|
|
|
{
|
2011-12-26 12:18:01 +00:00
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_REGULAR):
|
2011-10-25 16:09:00 +00:00
|
|
|
fragment = grub_le_to_cpu32 (ino->ino.long_file.fragment);
|
2011-12-26 12:18:01 +00:00
|
|
|
break;
|
|
|
|
case grub_cpu_to_le16_compile_time (SQUASH_TYPE_REGULAR):
|
2011-10-25 16:09:00 +00:00
|
|
|
fragment = grub_le_to_cpu32 (ino->ino.file.fragment);
|
2011-12-26 12:18:01 +00:00
|
|
|
break;
|
2010-12-09 16:06:49 +00:00
|
|
|
}
|
2010-12-08 20:22:46 +00:00
|
|
|
|
2017-02-24 16:10:43 +00:00
|
|
|
/* Squash may pack file tail as fragment. So read initial part directly and
|
|
|
|
get tail from fragments */
|
|
|
|
direct_len = fragment == 0xffffffff ? file->size : file->size & ~mask;
|
|
|
|
if (off < direct_len)
|
|
|
|
{
|
|
|
|
grub_size_t read_len = direct_len - off;
|
|
|
|
grub_ssize_t res;
|
|
|
|
|
|
|
|
if (read_len > len)
|
|
|
|
read_len = len;
|
|
|
|
res = direct_read (data, ino, off, buf, read_len);
|
|
|
|
if ((grub_size_t) res != read_len)
|
|
|
|
return -1; /* FIXME: is short read possible here? */
|
|
|
|
len -= read_len;
|
|
|
|
if (!len)
|
|
|
|
return read_len;
|
|
|
|
buf += read_len;
|
|
|
|
off = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
off -= direct_len;
|
2011-10-25 16:09:00 +00:00
|
|
|
|
|
|
|
err = read_chunk (data, &frag, sizeof (frag),
|
|
|
|
data->fragments, sizeof (frag) * fragment);
|
|
|
|
if (err)
|
|
|
|
return -1;
|
2017-02-24 16:10:43 +00:00
|
|
|
a = grub_le_to_cpu64 (frag.offset);
|
2012-05-22 06:38:17 +00:00
|
|
|
compressed = !(frag.size & grub_cpu_to_le32_compile_time (SQUASH_BLOCK_UNCOMPRESSED));
|
2011-10-25 16:09:00 +00:00
|
|
|
if (ino->ino.type == grub_cpu_to_le16_compile_time (SQUASH_TYPE_LONG_REGULAR))
|
2012-05-04 15:13:24 +00:00
|
|
|
b = grub_le_to_cpu32 (ino->ino.long_file.offset) + off;
|
2011-10-25 16:09:00 +00:00
|
|
|
else
|
|
|
|
b = grub_le_to_cpu32 (ino->ino.file.offset) + off;
|
|
|
|
|
2010-12-09 19:41:41 +00:00
|
|
|
/* FIXME: cache uncompressed chunks. */
|
|
|
|
if (compressed)
|
2011-12-26 12:18:01 +00:00
|
|
|
{
|
|
|
|
char *block;
|
2012-05-22 06:38:17 +00:00
|
|
|
block = grub_malloc (grub_le_to_cpu32 (frag.size));
|
2011-12-26 12:18:01 +00:00
|
|
|
if (!block)
|
|
|
|
return -1;
|
|
|
|
err = grub_disk_read (data->disk,
|
|
|
|
a >> GRUB_DISK_SECTOR_BITS,
|
|
|
|
a & (GRUB_DISK_SECTOR_SIZE - 1),
|
2012-05-22 06:38:17 +00:00
|
|
|
grub_le_to_cpu32 (frag.size), block);
|
2011-12-26 12:18:01 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (block);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-05-22 06:38:17 +00:00
|
|
|
if (data->decompress (block, grub_le_to_cpu32 (frag.size),
|
|
|
|
b, buf, len, data)
|
2011-12-26 12:18:01 +00:00
|
|
|
!= (grub_ssize_t) len)
|
|
|
|
{
|
|
|
|
grub_free (block);
|
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "incorrect compressed chunk");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
grub_free (block);
|
|
|
|
}
|
2010-12-09 19:41:41 +00:00
|
|
|
else
|
2011-12-26 12:18:01 +00:00
|
|
|
{
|
|
|
|
err = grub_disk_read (data->disk, (a + b) >> GRUB_DISK_SECTOR_BITS,
|
2010-12-09 19:41:41 +00:00
|
|
|
(a + b) & (GRUB_DISK_SECTOR_SIZE - 1), len, buf);
|
2011-12-26 12:18:01 +00:00
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-24 16:10:43 +00:00
|
|
|
return orig_len;
|
2010-12-09 19:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_squash_close (grub_file_t file)
|
|
|
|
{
|
2011-11-09 14:01:58 +00:00
|
|
|
squash_unmount (file->data);
|
2010-12-08 20:22:46 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-12-11 17:20:45 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_squash_mtime (grub_device_t dev, grub_int32_t *tm)
|
|
|
|
{
|
|
|
|
struct grub_squash_data *data = 0;
|
|
|
|
|
|
|
|
data = squash_mount (dev->disk);
|
|
|
|
if (! data)
|
|
|
|
return grub_errno;
|
|
|
|
*tm = grub_le_to_cpu32 (data->sb.creation_time);
|
2011-11-09 14:01:58 +00:00
|
|
|
squash_unmount (data);
|
2010-12-11 17:20:45 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-12-08 20:22:46 +00:00
|
|
|
static struct grub_fs grub_squash_fs =
|
|
|
|
{
|
|
|
|
.name = "squash4",
|
2019-04-08 05:24:24 +00:00
|
|
|
.fs_dir = grub_squash_dir,
|
|
|
|
.fs_open = grub_squash_open,
|
|
|
|
.fs_read = grub_squash_read,
|
|
|
|
.fs_close = grub_squash_close,
|
|
|
|
.fs_mtime = grub_squash_mtime,
|
2010-12-08 20:22:46 +00:00
|
|
|
#ifdef GRUB_UTIL
|
|
|
|
.reserved_first_sector = 0,
|
2012-02-27 20:36:58 +00:00
|
|
|
.blocklist_install = 0,
|
2010-12-08 20:22:46 +00:00
|
|
|
#endif
|
|
|
|
.next = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(squash4)
|
|
|
|
{
|
|
|
|
grub_fs_register (&grub_squash_fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(squash4)
|
|
|
|
{
|
|
|
|
grub_fs_unregister (&grub_squash_fs);
|
|
|
|
}
|
|
|
|
|