/* cpio.c - cpio and tar filesystem. */ /* * VAS_EBOOT -- 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 . */ #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); struct VasEBoot_archelp_data { VasEBoot_disk_t disk; VasEBoot_off_t hofs; VasEBoot_off_t next_hofs; VasEBoot_off_t dofs; VasEBoot_off_t size; }; #if __GNUC__ >= 9 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Waddress-of-packed-member" #endif static VasEBoot_err_t VasEBoot_cpio_find_file (struct VasEBoot_archelp_data *data, char **name, VasEBoot_int32_t *mtime, VasEBoot_uint32_t *mode) { struct head hd; VasEBoot_size_t namesize; VasEBoot_uint32_t modeval; VasEBoot_size_t sz; data->hofs = data->next_hofs; if (VasEBoot_disk_read (data->disk, 0, data->hofs, sizeof (hd), &hd)) return VasEBoot_errno; if (VasEBoot_memcmp (hd.magic, MAGIC, sizeof (hd.magic)) != 0 #ifdef MAGIC2 && VasEBoot_memcmp (hd.magic, MAGIC2, sizeof (hd.magic)) != 0 #endif ) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "invalid cpio archive"); if (VasEBoot_cast (read_number (hd.filesize, ARRAY_SIZE (hd.filesize)), &data->size)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, N_("data size overflow")); if (mtime) { if (VasEBoot_cast (read_number (hd.mtime, ARRAY_SIZE (hd.mtime)), mtime)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, N_("mtime overflow")); } if (VasEBoot_cast (read_number (hd.mode, ARRAY_SIZE (hd.mode)), &modeval)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, N_("mode overflow")); if (VasEBoot_cast (read_number (hd.namesize, ARRAY_SIZE (hd.namesize)), &namesize)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, N_("namesize overflow")); /* Don't allow negative numbers. */ if (namesize >= 0x80000000) { /* Probably a corruption, don't attempt to recover. */ *mode = VAS_EBOOT_ARCHELP_ATTR_END; return VAS_EBOOT_ERR_NONE; } *mode = modeval; if (VasEBoot_add (namesize, 1, &sz)) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("file name size overflow")); *name = VasEBoot_malloc (sz); if (*name == NULL) return VasEBoot_errno; if (VasEBoot_disk_read (data->disk, 0, data->hofs + sizeof (hd), namesize, *name)) { VasEBoot_free (*name); return VasEBoot_errno; } (*name)[namesize] = 0; if (data->size == 0 && modeval == 0 && namesize == 11 && VasEBoot_memcmp(*name, "TRAILER!!!", 11) == 0) { *mode = VAS_EBOOT_ARCHELP_ATTR_END; VasEBoot_free (*name); return VAS_EBOOT_ERR_NONE; } data->dofs = data->hofs + ALIGN_CPIO (sizeof (hd) + namesize); data->next_hofs = data->dofs + ALIGN_CPIO (data->size); return VAS_EBOOT_ERR_NONE; } #if __GNUC__ >= 9 #pragma GCC diagnostic pop #endif static char * VasEBoot_cpio_get_link_target (struct VasEBoot_archelp_data *data) { char *ret; VasEBoot_err_t err; VasEBoot_size_t sz; if (data->size == 0) return VasEBoot_strdup (""); if (VasEBoot_add (data->size, 1, &sz)) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("target data size overflow")); return NULL; } ret = VasEBoot_malloc (sz); if (!ret) return NULL; err = VasEBoot_disk_read (data->disk, 0, data->dofs, data->size, ret); if (err) { VasEBoot_free (ret); return NULL; } ret[data->size] = '\0'; return ret; } static void VasEBoot_cpio_rewind (struct VasEBoot_archelp_data *data) { data->next_hofs = 0; } static struct VasEBoot_archelp_ops arcops = { .find_file = VasEBoot_cpio_find_file, .get_link_target = VasEBoot_cpio_get_link_target, .rewind = VasEBoot_cpio_rewind }; static struct VasEBoot_archelp_data * VasEBoot_cpio_mount (VasEBoot_disk_t disk) { struct head hd; struct VasEBoot_archelp_data *data; if (VasEBoot_disk_read (disk, 0, 0, sizeof (hd), &hd)) goto fail; if (VasEBoot_memcmp (hd.magic, MAGIC, sizeof (MAGIC) - 1) #ifdef MAGIC2 && VasEBoot_memcmp (hd.magic, MAGIC2, sizeof (MAGIC2) - 1) #endif ) goto fail; data = (struct VasEBoot_archelp_data *) VasEBoot_zalloc (sizeof (*data)); if (!data) goto fail; data->disk = disk; return data; fail: VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "not a " FSNAME " filesystem"); return 0; } static VasEBoot_err_t VasEBoot_cpio_dir (VasEBoot_device_t device, const char *path_in, VasEBoot_fs_dir_hook_t hook, void *hook_data) { struct VasEBoot_archelp_data *data; VasEBoot_err_t err; data = VasEBoot_cpio_mount (device->disk); if (!data) return VasEBoot_errno; err = VasEBoot_archelp_dir (data, &arcops, path_in, hook, hook_data); VasEBoot_free (data); return err; } static VasEBoot_err_t VasEBoot_cpio_open (VasEBoot_file_t file, const char *name_in) { struct VasEBoot_archelp_data *data; VasEBoot_err_t err; data = VasEBoot_cpio_mount (file->device->disk); if (!data) return VasEBoot_errno; err = VasEBoot_archelp_open (data, &arcops, name_in); if (err) { VasEBoot_free (data); } else { file->data = data; file->size = data->size; } return err; } static VasEBoot_ssize_t VasEBoot_cpio_read (VasEBoot_file_t file, char *buf, VasEBoot_size_t len) { struct VasEBoot_archelp_data *data; VasEBoot_ssize_t ret; data = file->data; data->disk->read_hook = file->read_hook; data->disk->read_hook_data = file->read_hook_data; ret = (VasEBoot_disk_read (data->disk, 0, data->dofs + file->offset, len, buf)) ? -1 : (VasEBoot_ssize_t) len; data->disk->read_hook = 0; return ret; } static VasEBoot_err_t VasEBoot_cpio_close (VasEBoot_file_t file) { struct VasEBoot_archelp_data *data; data = file->data; VasEBoot_free (data); return VasEBoot_errno; } static struct VasEBoot_fs VasEBoot_cpio_fs = { .name = FSNAME, .fs_dir = VasEBoot_cpio_dir, .fs_open = VasEBoot_cpio_open, .fs_read = VasEBoot_cpio_read, .fs_close = VasEBoot_cpio_close, #ifdef VAS_EBOOT_UTIL .reserved_first_sector = 0, .blocklist_install = 0, #endif };