1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/* cpio.c - cpio and tar filesystem. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2013 Free Software Foundation, Inc.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/i18n.h>
#include <grub/archelp.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+");
struct grub_archelp_data
{
grub_disk_t disk;
grub_off_t hofs;
grub_off_t next_hofs;
grub_off_t dofs;
grub_off_t size;
};
#if __GNUC__ >= 9
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
#endif
static grub_err_t
grub_cpio_find_file (struct grub_archelp_data *data, char **name,
grub_int32_t *mtime, grub_uint32_t *mode)
{
struct head hd;
grub_size_t namesize;
grub_uint32_t modeval;
grub_size_t sz;
data->hofs = data->next_hofs;
if (grub_disk_read (data->disk, 0, data->hofs, sizeof (hd), &hd))
return grub_errno;
if (grub_memcmp (hd.magic, MAGIC, sizeof (hd.magic)) != 0
#ifdef MAGIC2
&& grub_memcmp (hd.magic, MAGIC2, sizeof (hd.magic)) != 0
#endif
)
return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive");
if (grub_cast (read_number (hd.filesize, ARRAY_SIZE (hd.filesize)), &data->size))
return grub_error (GRUB_ERR_BAD_FS, N_("data size overflow"));
if (mtime)
{
if (grub_cast (read_number (hd.mtime, ARRAY_SIZE (hd.mtime)), mtime))
return grub_error (GRUB_ERR_BAD_FS, N_("mtime overflow"));
}
if (grub_cast (read_number (hd.mode, ARRAY_SIZE (hd.mode)), &modeval))
return grub_error (GRUB_ERR_BAD_FS, N_("mode overflow"));
if (grub_cast (read_number (hd.namesize, ARRAY_SIZE (hd.namesize)), &namesize))
return grub_error (GRUB_ERR_BAD_FS, N_("namesize overflow"));
/* Don't allow negative numbers. */
if (namesize >= 0x80000000)
{
/* Probably a corruption, don't attempt to recover. */
*mode = GRUB_ARCHELP_ATTR_END;
return GRUB_ERR_NONE;
}
*mode = modeval;
if (grub_add (namesize, 1, &sz))
return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("file name size overflow"));
*name = grub_malloc (sz);
if (*name == NULL)
return grub_errno;
if (grub_disk_read (data->disk, 0, data->hofs + sizeof (hd),
namesize, *name))
{
grub_free (*name);
return grub_errno;
}
(*name)[namesize] = 0;
if (data->size == 0 && modeval == 0 && namesize == 11
&& grub_memcmp(*name, "TRAILER!!!", 11) == 0)
{
*mode = GRUB_ARCHELP_ATTR_END;
grub_free (*name);
return GRUB_ERR_NONE;
}
data->dofs = data->hofs + ALIGN_CPIO (sizeof (hd) + namesize);
data->next_hofs = data->dofs + ALIGN_CPIO (data->size);
return GRUB_ERR_NONE;
}
#if __GNUC__ >= 9
#pragma GCC diagnostic pop
#endif
static char *
grub_cpio_get_link_target (struct grub_archelp_data *data)
{
char *ret;
grub_err_t err;
grub_size_t sz;
if (data->size == 0)
return grub_strdup ("");
if (grub_add (data->size, 1, &sz))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, N_("target data size overflow"));
return NULL;
}
ret = grub_malloc (sz);
if (!ret)
return NULL;
err = grub_disk_read (data->disk, 0, data->dofs, data->size,
ret);
if (err)
{
grub_free (ret);
return NULL;
}
ret[data->size] = '\0';
return ret;
}
static void
grub_cpio_rewind (struct grub_archelp_data *data)
{
data->next_hofs = 0;
}
static struct grub_archelp_ops arcops =
{
.find_file = grub_cpio_find_file,
.get_link_target = grub_cpio_get_link_target,
.rewind = grub_cpio_rewind
};
static struct grub_archelp_data *
grub_cpio_mount (grub_disk_t disk)
{
struct head hd;
struct grub_archelp_data *data;
if (grub_disk_read (disk, 0, 0, sizeof (hd), &hd))
goto fail;
if (grub_memcmp (hd.magic, MAGIC, sizeof (MAGIC) - 1)
#ifdef MAGIC2
&& grub_memcmp (hd.magic, MAGIC2, sizeof (MAGIC2) - 1)
#endif
)
goto fail;
data = (struct grub_archelp_data *) grub_zalloc (sizeof (*data));
if (!data)
goto fail;
data->disk = disk;
return data;
fail:
grub_error (GRUB_ERR_BAD_FS, "not a " FSNAME " filesystem");
return 0;
}
static grub_err_t
grub_cpio_dir (grub_device_t device, const char *path_in,
grub_fs_dir_hook_t hook, void *hook_data)
{
struct grub_archelp_data *data;
grub_err_t err;
data = grub_cpio_mount (device->disk);
if (!data)
return grub_errno;
err = grub_archelp_dir (data, &arcops,
path_in, hook, hook_data);
grub_free (data);
return err;
}
static grub_err_t
grub_cpio_open (grub_file_t file, const char *name_in)
{
struct grub_archelp_data *data;
grub_err_t err;
data = grub_cpio_mount (file->device->disk);
if (!data)
return grub_errno;
err = grub_archelp_open (data, &arcops, name_in);
if (err)
{
grub_free (data);
}
else
{
file->data = data;
file->size = data->size;
}
return err;
}
static grub_ssize_t
grub_cpio_read (grub_file_t file, char *buf, grub_size_t len)
{
struct grub_archelp_data *data;
grub_ssize_t ret;
data = file->data;
data->disk->read_hook = file->read_hook;
data->disk->read_hook_data = file->read_hook_data;
ret = (grub_disk_read (data->disk, 0, data->dofs + file->offset,
len, buf)) ? -1 : (grub_ssize_t) len;
data->disk->read_hook = 0;
return ret;
}
static grub_err_t
grub_cpio_close (grub_file_t file)
{
struct grub_archelp_data *data;
data = file->data;
grub_free (data);
return grub_errno;
}
static struct grub_fs grub_cpio_fs = {
.name = FSNAME,
.fs_dir = grub_cpio_dir,
.fs_open = grub_cpio_open,
.fs_read = grub_cpio_read,
.fs_close = grub_cpio_close,
#ifdef GRUB_UTIL
.reserved_first_sector = 0,
.blocklist_install = 0,
#endif
};
|