2007-12-26 07:51:19 +00:00
|
|
|
/* cpio.c - cpio and tar filesystem. */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2008-02-08 21:27:55 +00:00
|
|
|
* Copyright (C) 2007,2008 Free Software Foundation, Inc.
|
2007-12-26 07:51:19 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-05-13 19:16:55 +00:00
|
|
|
#ifndef MODE_USTAR
|
|
|
|
/* cpio support */
|
2007-12-26 07:51:19 +00:00
|
|
|
#define MAGIC_BCPIO 070707
|
2009-05-13 19:16:55 +00:00
|
|
|
struct head
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
|
|
|
grub_uint16_t magic;
|
|
|
|
grub_uint16_t dev;
|
|
|
|
grub_uint16_t ino;
|
|
|
|
grub_uint16_t mode;
|
|
|
|
grub_uint16_t uid;
|
|
|
|
grub_uint16_t gid;
|
|
|
|
grub_uint16_t nlink;
|
|
|
|
grub_uint16_t rdev;
|
|
|
|
grub_uint16_t mtime_1;
|
|
|
|
grub_uint16_t mtime_2;
|
|
|
|
grub_uint16_t namesize;
|
|
|
|
grub_uint16_t filesize_1;
|
|
|
|
grub_uint16_t filesize_2;
|
|
|
|
} __attribute__ ((packed));
|
2009-05-13 19:16:55 +00:00
|
|
|
#else
|
|
|
|
/* tar support */
|
2007-12-26 07:51:19 +00:00
|
|
|
#define MAGIC_USTAR "ustar"
|
2009-05-13 19:16:55 +00:00
|
|
|
struct head
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
|
|
|
char name[100];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12];
|
|
|
|
char mtime[12];
|
|
|
|
char chksum[8];
|
|
|
|
char typeflag;
|
|
|
|
char linkname[100];
|
|
|
|
char magic[6];
|
|
|
|
char version[2];
|
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
|
|
|
char devmajor[8];
|
|
|
|
char devminor[8];
|
|
|
|
char prefix[155];
|
|
|
|
} __attribute__ ((packed));
|
2009-05-13 19:16:55 +00:00
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
|
|
|
|
struct grub_cpio_data
|
|
|
|
{
|
|
|
|
grub_disk_t disk;
|
|
|
|
grub_uint32_t hofs;
|
|
|
|
grub_uint32_t dofs;
|
|
|
|
grub_uint32_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static grub_dl_t my_mod;
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cpio_find_file (struct grub_cpio_data *data, char **name,
|
|
|
|
grub_uint32_t * ofs)
|
|
|
|
{
|
2009-02-07 23:38:01 +00:00
|
|
|
#ifndef MODE_USTAR
|
2009-05-13 19:16:55 +00:00
|
|
|
struct head hd;
|
2007-12-26 07:51:19 +00:00
|
|
|
|
|
|
|
if (grub_disk_read
|
2009-05-13 18:58:38 +00:00
|
|
|
(data->disk, 0, data->hofs, sizeof (hd), &hd))
|
2007-12-26 07:51:19 +00:00
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
if (hd.magic != MAGIC_BCPIO)
|
|
|
|
return grub_error (GRUB_ERR_BAD_FS, "Invalid cpio archive");
|
|
|
|
|
|
|
|
data->size = (((grub_uint32_t) hd.filesize_1) << 16) + hd.filesize_2;
|
|
|
|
|
|
|
|
if (hd.namesize & 1)
|
|
|
|
hd.namesize++;
|
|
|
|
|
|
|
|
if ((*name = grub_malloc (hd.namesize)) == NULL)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
if (grub_disk_read (data->disk, 0, data->hofs + sizeof (hd),
|
|
|
|
hd.namesize, *name))
|
|
|
|
{
|
|
|
|
grub_free (*name);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2008-02-06 19:57:40 +00:00
|
|
|
if (data->size == 0 && hd.mode == 0 && hd.namesize == 11 + 1
|
|
|
|
&& ! grub_memcmp(*name, "TRAILER!!!", 11))
|
|
|
|
{
|
|
|
|
*ofs = 0;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2007-12-26 07:51:19 +00:00
|
|
|
data->dofs = data->hofs + sizeof (hd) + hd.namesize;
|
|
|
|
*ofs = data->dofs + data->size;
|
|
|
|
if (data->size & 1)
|
|
|
|
(*ofs)++;
|
2009-02-07 23:38:01 +00:00
|
|
|
#else
|
2009-05-13 19:16:55 +00:00
|
|
|
struct head hd;
|
2007-12-26 07:51:19 +00:00
|
|
|
|
|
|
|
if (grub_disk_read
|
2009-05-13 18:58:38 +00:00
|
|
|
(data->disk, 0, data->hofs, sizeof (hd), &hd))
|
2007-12-26 07:51:19 +00:00
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
if (!hd.name[0])
|
|
|
|
{
|
|
|
|
*ofs = 0;
|
2008-02-06 19:57:40 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2007-12-26 07:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (grub_memcmp (hd.magic, MAGIC_USTAR, sizeof (MAGIC_USTAR) - 1))
|
|
|
|
return grub_error (GRUB_ERR_BAD_FS, "Invalid tar archive");
|
|
|
|
|
|
|
|
if ((*name = grub_strdup (hd.name)) == NULL)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
data->size = grub_strtoul (hd.size, NULL, 8);
|
|
|
|
data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE;
|
|
|
|
*ofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) &
|
|
|
|
~(GRUB_DISK_SECTOR_SIZE - 1));
|
2009-02-07 23:38:01 +00:00
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_cpio_data *
|
|
|
|
grub_cpio_mount (grub_disk_t disk)
|
|
|
|
{
|
2009-05-13 19:16:55 +00:00
|
|
|
struct head hd;
|
2007-12-26 07:51:19 +00:00
|
|
|
struct grub_cpio_data *data;
|
|
|
|
|
2009-05-13 19:16:55 +00:00
|
|
|
if (grub_disk_read (disk, 0, 0, sizeof (hd), &hd))
|
2007-12-26 07:51:19 +00:00
|
|
|
goto fail;
|
|
|
|
|
2009-02-07 23:38:01 +00:00
|
|
|
#ifndef MODE_USTAR
|
2009-05-13 19:16:55 +00:00
|
|
|
if (hd.magic != MAGIC_BCPIO)
|
2009-02-07 23:38:01 +00:00
|
|
|
#else
|
2009-05-13 19:16:55 +00:00
|
|
|
if (grub_memcmp (hd.magic, MAGIC_USTAR,
|
|
|
|
sizeof (MAGIC_USTAR) - 1))
|
2009-02-07 23:38:01 +00:00
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data = (struct grub_cpio_data *) grub_malloc (sizeof (*data));
|
|
|
|
if (!data)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->disk = disk;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
fail:
|
2009-02-07 23:38:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FS, "not a "
|
|
|
|
#ifdef MODE_USTAR
|
|
|
|
"tar"
|
|
|
|
#else
|
|
|
|
"cpio"
|
|
|
|
#endif
|
|
|
|
" filesystem");
|
2007-12-26 07:51:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cpio_dir (grub_device_t device, const char *path,
|
2009-06-10 21:04:23 +00:00
|
|
|
int (*hook) (const char *filename,
|
2009-04-05 20:19:05 +00:00
|
|
|
const struct grub_dirhook_info *info))
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
|
|
|
struct grub_cpio_data *data;
|
|
|
|
grub_uint32_t ofs;
|
2008-02-06 19:57:40 +00:00
|
|
|
char *prev, *name;
|
|
|
|
const char *np;
|
2007-12-26 07:51:19 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
grub_dl_ref (my_mod);
|
|
|
|
|
|
|
|
prev = 0;
|
|
|
|
|
|
|
|
data = grub_cpio_mount (device->disk);
|
|
|
|
if (!data)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
np = path + 1;
|
|
|
|
len = grub_strlen (path) - 1;
|
|
|
|
|
|
|
|
data->hofs = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (grub_cpio_find_file (data, &name, &ofs))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!ofs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (grub_memcmp (np, name, len) == 0)
|
|
|
|
{
|
|
|
|
char *p, *n;
|
|
|
|
|
|
|
|
n = name + len;
|
|
|
|
if (*n == '/')
|
|
|
|
n++;
|
|
|
|
|
|
|
|
p = grub_strchr (name + len, '/');
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
if ((!prev) || (grub_strcmp (prev, name) != 0))
|
|
|
|
{
|
2009-04-05 20:19:05 +00:00
|
|
|
struct grub_dirhook_info info;
|
|
|
|
grub_memset (&info, 0, sizeof (info));
|
|
|
|
info.dir = (p != NULL);
|
|
|
|
|
|
|
|
hook (name + len, &info);
|
2007-12-26 07:51:19 +00:00
|
|
|
if (prev)
|
|
|
|
grub_free (prev);
|
|
|
|
prev = name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
grub_free (name);
|
|
|
|
}
|
|
|
|
data->hofs = ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
grub_free (prev);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
grub_free (data);
|
|
|
|
|
|
|
|
grub_dl_unref (my_mod);
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cpio_open (grub_file_t file, const char *name)
|
|
|
|
{
|
|
|
|
struct grub_cpio_data *data;
|
|
|
|
grub_uint32_t ofs;
|
|
|
|
char *fn;
|
2008-11-14 20:08:47 +00:00
|
|
|
int i, j;
|
2007-12-26 07:51:19 +00:00
|
|
|
|
|
|
|
grub_dl_ref (my_mod);
|
|
|
|
|
|
|
|
data = grub_cpio_mount (file->device->disk);
|
|
|
|
if (!data)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->hofs = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (grub_cpio_find_file (data, &fn, &ofs))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!ofs)
|
2008-02-06 19:57:40 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
|
|
|
|
break;
|
|
|
|
}
|
2007-12-26 07:51:19 +00:00
|
|
|
|
2008-11-14 20:08:47 +00:00
|
|
|
/* Compare NAME and FN by hand in order to cope with duplicate
|
|
|
|
slashes. */
|
|
|
|
i = 1;
|
|
|
|
j = 0;
|
|
|
|
while (1)
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
2008-11-14 20:08:47 +00:00
|
|
|
if (name[i] != fn[j])
|
|
|
|
goto no_match;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-11-14 20:08:47 +00:00
|
|
|
if (name[i] == '\0')
|
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-11-14 20:08:47 +00:00
|
|
|
if (name[i] == '/' && name[i+1] == '/')
|
|
|
|
i++;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-11-14 20:08:47 +00:00
|
|
|
i++;
|
|
|
|
j++;
|
2007-12-26 07:51:19 +00:00
|
|
|
}
|
|
|
|
|
2008-11-14 20:08:47 +00:00
|
|
|
file->data = data;
|
|
|
|
file->size = data->size;
|
|
|
|
grub_free (fn);
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
no_match:
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2007-12-26 07:51:19 +00:00
|
|
|
grub_free (fn);
|
|
|
|
data->hofs = ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
grub_free (data);
|
|
|
|
|
|
|
|
grub_dl_unref (my_mod);
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_ssize_t
|
|
|
|
grub_cpio_read (grub_file_t file, char *buf, grub_size_t len)
|
|
|
|
{
|
|
|
|
struct grub_cpio_data *data;
|
|
|
|
|
|
|
|
data = file->data;
|
|
|
|
return (grub_disk_read (data->disk, 0, data->dofs + file->offset,
|
2008-04-02 06:10:44 +00:00
|
|
|
len, buf)) ? -1 : (grub_ssize_t) len;
|
2007-12-26 07:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cpio_close (grub_file_t file)
|
|
|
|
{
|
|
|
|
grub_free (file->data);
|
|
|
|
|
|
|
|
grub_dl_unref (my_mod);
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_fs grub_cpio_fs = {
|
2009-02-07 23:38:01 +00:00
|
|
|
#ifdef MODE_USTAR
|
|
|
|
.name = "tarfs",
|
|
|
|
#else
|
2007-12-26 07:51:19 +00:00
|
|
|
.name = "cpiofs",
|
2009-02-07 23:38:01 +00:00
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
.dir = grub_cpio_dir,
|
|
|
|
.open = grub_cpio_open,
|
|
|
|
.read = grub_cpio_read,
|
|
|
|
.close = grub_cpio_close,
|
|
|
|
};
|
|
|
|
|
2009-02-07 23:38:01 +00:00
|
|
|
#ifdef MODE_USTAR
|
2007-12-26 07:51:19 +00:00
|
|
|
GRUB_MOD_INIT (cpio)
|
2009-02-07 23:38:01 +00:00
|
|
|
#else
|
|
|
|
GRUB_MOD_INIT (tar)
|
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
|
|
|
grub_fs_register (&grub_cpio_fs);
|
|
|
|
my_mod = mod;
|
|
|
|
}
|
|
|
|
|
2009-02-07 23:38:01 +00:00
|
|
|
#ifdef MODE_USTAR
|
2007-12-26 07:51:19 +00:00
|
|
|
GRUB_MOD_FINI (cpio)
|
2009-02-07 23:38:01 +00:00
|
|
|
#else
|
|
|
|
GRUB_MOD_FINI (tar)
|
|
|
|
#endif
|
2007-12-26 07:51:19 +00:00
|
|
|
{
|
|
|
|
grub_fs_unregister (&grub_cpio_fs);
|
|
|
|
}
|