romfs.c 11.8 KB
Newer Older
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
/*
 *  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/file.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/disk.h>
#include <grub/fs.h>
#include <grub/fshelp.h>

27 28
GRUB_MOD_LICENSE ("GPLv3+");

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
struct grub_romfs_superblock
{
  char magic[8];
#define GRUB_ROMFS_MAGIC "-rom1fs-"
  grub_uint32_t total_size;
  grub_uint32_t chksum;
  char label[0];
};

struct grub_romfs_file_header
{
  grub_uint32_t next_file;
  grub_uint32_t spec;
  grub_uint32_t size;
  grub_uint32_t chksum;
  char name[0];
};

struct grub_romfs_data
{
  grub_disk_addr_t first_file;
  grub_disk_t disk;
};

struct grub_fshelp_node
{
  grub_disk_addr_t addr;
  struct grub_romfs_data *data;
  grub_disk_addr_t data_addr;
58
  /* Not filled for root.  */
59 60 61 62 63
  struct grub_romfs_file_header file;
};

#define GRUB_ROMFS_ALIGN 16
#define GRUB_ROMFS_TYPE_MASK 7
64
#define GRUB_ROMFS_TYPE_HARDLINK 0
65
#define GRUB_ROMFS_TYPE_DIRECTORY 1
66
#define GRUB_ROMFS_TYPE_REGULAR 2
67
#define GRUB_ROMFS_TYPE_SYMLINK 3
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

static grub_err_t
do_checksum (void *in, grub_size_t insize)
{
  grub_uint32_t *a = in;
  grub_size_t sz = insize / 4;
  grub_uint32_t *b = a + sz;
  grub_uint32_t csum = 0;

  while (a < b)
    csum += grub_be_to_cpu32 (*a++);
  if (csum)
    return grub_error (GRUB_ERR_BAD_FS, "invalid checksum");
  return GRUB_ERR_NONE;
}

static struct grub_romfs_data *
grub_romfs_mount (grub_device_t dev)
{
  union {
    struct grub_romfs_superblock sb;
    char d[512];
  } sb;
  grub_err_t err;
  char *ptr;
  grub_disk_addr_t sec = 0;
  struct grub_romfs_data *data;
  if (!dev->disk)
    {
      grub_error (GRUB_ERR_BAD_FS, "not a disk");
      return NULL;
    }
  err = grub_disk_read (dev->disk, 0, 0, sizeof (sb), &sb);
  if (err == GRUB_ERR_OUT_OF_RANGE)
    err = grub_errno = GRUB_ERR_BAD_FS;
  if (err)
    return NULL;
  if (grub_be_to_cpu32 (sb.sb.total_size) < sizeof (sb))
    {
      grub_error (GRUB_ERR_BAD_FS, "too short filesystem");
      return NULL;
    }
110 111 112 113 114 115
  if (grub_memcmp (sb.sb.magic, GRUB_ROMFS_MAGIC,
		   sizeof (sb.sb.magic)) != 0)
    {
      grub_error (GRUB_ERR_BAD_FS, "not romfs");
      return NULL;
    }    
116 117 118
  err = do_checksum (&sb, sizeof (sb) < grub_be_to_cpu32 (sb.sb.total_size) ?
		     sizeof (sb) : grub_be_to_cpu32 (sb.sb.total_size));
  if (err)
119 120 121 122
    {
      grub_error (GRUB_ERR_BAD_FS, "checksum incorrect");
      return NULL;
    }
123
  for (ptr = sb.sb.label; (void *) ptr < (void *) (&sb + 1)
124
	 && ptr - sb.d < (grub_ssize_t) grub_be_to_cpu32 (sb.sb.total_size); ptr++)
125 126
    if (!*ptr)
      break;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  while ((void *) ptr == &sb + 1)
    {
      sec++;
      err = grub_disk_read (dev->disk, sec, 0, sizeof (sb), &sb);
      if (err == GRUB_ERR_OUT_OF_RANGE)
	err = grub_errno = GRUB_ERR_BAD_FS;
      if (err)
	return NULL;
      for (ptr = sb.d; (void *) ptr < (void *) (&sb + 1)
	     && (ptr - sb.d + (sec << GRUB_DISK_SECTOR_BITS)
		 < grub_be_to_cpu32 (sb.sb.total_size));
	   ptr++)
	if (!*ptr)
	  break;
    }
142 143 144
  data = grub_malloc (sizeof (*data));
  if (!data)
    return NULL;
145 146
  data->first_file = ALIGN_UP (ptr + 1 - sb.d, GRUB_ROMFS_ALIGN)
    + (sec << GRUB_DISK_SECTOR_BITS);
147 148 149 150
  data->disk = dev->disk;
  return data;
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
static char *
grub_romfs_read_symlink (grub_fshelp_node_t node)
{
  char *ret;
  grub_err_t err;
  ret = grub_malloc (grub_be_to_cpu32 (node->file.size) + 1);
  if (!ret)
    return NULL;
  err = grub_disk_read (node->data->disk,
			(node->data_addr) >> GRUB_DISK_SECTOR_BITS,
			(node->data_addr) & (GRUB_DISK_SECTOR_SIZE - 1),
			grub_be_to_cpu32 (node->file.size), ret);
  if (err)
    {
      grub_free (ret);
      return NULL;
    }
  ret[grub_be_to_cpu32 (node->file.size)] = 0;
  return ret;
}

172 173
static int
grub_romfs_iterate_dir (grub_fshelp_node_t dir,
174
			grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
175 176 177 178 179
{
  grub_disk_addr_t caddr;
  struct grub_romfs_file_header hdr;
  unsigned nptr;
  unsigned i, j;
180 181 182
  grub_size_t a = 0;
  grub_properly_aligned_t *name = NULL;

183
  for (caddr = dir->data_addr; caddr;
184 185 186 187 188
       caddr = grub_be_to_cpu32 (hdr.next_file) & ~(GRUB_ROMFS_ALIGN - 1))
    {
      grub_disk_addr_t naddr = caddr + sizeof (hdr);
      grub_uint32_t csum = 0;
      enum grub_fshelp_filetype filetype = GRUB_FSHELP_UNKNOWN;
189
      struct grub_fshelp_node *node = NULL;
190 191 192 193 194 195 196 197 198 199 200 201
      grub_err_t err;

      err = grub_disk_read (dir->data->disk, caddr >> GRUB_DISK_SECTOR_BITS,
			    caddr & (GRUB_DISK_SECTOR_SIZE - 1),
			    sizeof (hdr), &hdr);
      if (err)
	{
	  grub_free (name);
	  return 1;
	}
      for (nptr = 0; ; nptr++, naddr += 16)
	{
202
	  if (a <= nptr)
203
	    {
204
	      grub_properly_aligned_t *on;
205 206 207 208 209 210 211 212 213
	      a = 2 * (nptr + 1);
	      on = name;
	      name = grub_realloc (name, a * 16);
	      if (!name)
		{
		  grub_free (on);
		  return 1;
		}
	    }
214
	  COMPILE_TIME_ASSERT (16 % sizeof (name[0]) == 0);
215 216
	  err = grub_disk_read (dir->data->disk, naddr >> GRUB_DISK_SECTOR_BITS,
				naddr & (GRUB_DISK_SECTOR_SIZE - 1),
217
				16, name + (16 / sizeof (name[0])) * nptr);
218
	  if (err)
219
	    return 1;
220
	  for (j = 0; j < 16; j++)
221
	    if (!((char *) name)[16 * nptr + j])
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
	      break;
	  if (j != 16)
	    break;
	}
      for (i = 0; i < sizeof (hdr) / sizeof (grub_uint32_t); i++)
	csum += grub_be_to_cpu32 (((grub_uint32_t *) &hdr)[i]);
      for (i = 0; i < (nptr + 1) * 4; i++)
	csum += grub_be_to_cpu32 (((grub_uint32_t *) name)[i]);
      if (csum != 0)
	{
	  grub_error (GRUB_ERR_BAD_FS, "invalid checksum");
	  grub_free (name);
	  return 1;
	}
      node = grub_malloc (sizeof (*node));
      if (!node)
	return 1;
      node->addr = caddr;
      node->data_addr = caddr + (nptr + 1) * 16 + sizeof (hdr);
      node->data = dir->data;
      node->file = hdr;
      switch (grub_be_to_cpu32 (hdr.next_file) & GRUB_ROMFS_TYPE_MASK)
	{
	case GRUB_ROMFS_TYPE_REGULAR:
	  filetype = GRUB_FSHELP_REG;
	  break;
248 249 250
	case GRUB_ROMFS_TYPE_SYMLINK:
	  filetype = GRUB_FSHELP_SYMLINK;
	  break;
251
	case GRUB_ROMFS_TYPE_DIRECTORY:
252
	  node->data_addr = grub_be_to_cpu32 (hdr.spec);
253 254
	  filetype = GRUB_FSHELP_DIR;
	  break;
255 256 257 258 259 260 261 262 263 264 265
	case GRUB_ROMFS_TYPE_HARDLINK:
	  {
	    grub_disk_addr_t laddr;
	    node->addr = laddr = grub_be_to_cpu32 (hdr.spec);
	    err = grub_disk_read (dir->data->disk,
				  laddr >> GRUB_DISK_SECTOR_BITS,
				  laddr & (GRUB_DISK_SECTOR_SIZE - 1),
				  sizeof (node->file), &node->file);
	    if (err)
	      return 1;
	    if ((grub_be_to_cpu32 (node->file.next_file) & GRUB_ROMFS_TYPE_MASK)
266 267 268
		== GRUB_ROMFS_TYPE_REGULAR
		|| (grub_be_to_cpu32 (node->file.next_file)
		    & GRUB_ROMFS_TYPE_MASK) == GRUB_ROMFS_TYPE_SYMLINK)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	      {
		laddr += sizeof (hdr);
		while (1)
		  {
		    char buf[16];
		    err = grub_disk_read (dir->data->disk, 
					  laddr >> GRUB_DISK_SECTOR_BITS,
					  laddr & (GRUB_DISK_SECTOR_SIZE - 1),
					  16, buf);
		    if (err)
		      return 1;
		    for (i = 0; i < 16; i++)
		      if (!buf[i])
			break;
		    if (i != 16)
		      break;
		    laddr += 16;
		  }
		node->data_addr = laddr + 16;
	      }
289 290 291 292 293 294 295 296 297 298 299 300 301 302
	    if ((grub_be_to_cpu32 (node->file.next_file)
		 & GRUB_ROMFS_TYPE_MASK) == GRUB_ROMFS_TYPE_REGULAR)
	      filetype = GRUB_FSHELP_REG;
	    if ((grub_be_to_cpu32 (node->file.next_file)
		 & GRUB_ROMFS_TYPE_MASK) == GRUB_ROMFS_TYPE_SYMLINK)
	      filetype = GRUB_FSHELP_SYMLINK;
	    if ((grub_be_to_cpu32 (node->file.next_file) & GRUB_ROMFS_TYPE_MASK)
		== GRUB_ROMFS_TYPE_DIRECTORY)
	      {
		node->data_addr = grub_be_to_cpu32 (node->file.spec);
		filetype = GRUB_FSHELP_DIR;
	      }
	    
	    break;
303
	  }
304 305
	}

306
      if (hook ((char *) name, filetype, node, hook_data))
307 308 309 310 311 312 313 314 315
	{
	  grub_free (name);
	  return 1;
	}
    }
  grub_free (name);
  return 0;
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/* Context for grub_romfs_dir.  */
struct grub_romfs_dir_ctx
{
  grub_fs_dir_hook_t hook;
  void *hook_data;
};

/* Helper for grub_romfs_dir.  */
static int
grub_romfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
		     grub_fshelp_node_t node, void *data)
{
  struct grub_romfs_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);
  grub_free (node);
  return ctx->hook (filename, &info, ctx->hook_data);
}

338 339
static grub_err_t
grub_romfs_dir (grub_device_t device, const char *path,
340
		grub_fs_dir_hook_t hook, void *hook_data)
341
{
342
  struct grub_romfs_dir_ctx ctx = { hook, hook_data };
343 344 345 346 347 348 349 350
  struct grub_romfs_data *data = 0;
  struct grub_fshelp_node *fdiro = 0, start;

  data = grub_romfs_mount (device);
  if (! data)
    goto fail;

  start.addr = data->first_file;
351
  start.data_addr = data->first_file;
352 353
  start.data = data;
  grub_fshelp_find_file (path, &start, &fdiro, grub_romfs_iterate_dir,
354
			 grub_romfs_read_symlink, GRUB_FSHELP_DIR);
355 356 357
  if (grub_errno)
    goto fail;

358
  grub_romfs_iterate_dir (fdiro, grub_romfs_dir_iter, &ctx);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

 fail:
  grub_free (data);

  return grub_errno;
}

static grub_err_t
grub_romfs_open (struct grub_file *file, const char *name)
{
  struct grub_romfs_data *data = 0;
  struct grub_fshelp_node *fdiro = 0, start;

  data = grub_romfs_mount (file->device);
  if (! data)
    goto fail;

  start.addr = data->first_file;
377
  start.data_addr = data->first_file;
378 379 380
  start.data = data;

  grub_fshelp_find_file (name, &start, &fdiro, grub_romfs_iterate_dir,
381
			 grub_romfs_read_symlink, GRUB_FSHELP_REG);
382 383 384 385 386
  if (grub_errno)
    goto fail;

  file->size = grub_be_to_cpu32 (fdiro->file.size);
  file->data = fdiro;
387
  return GRUB_ERR_NONE;
388 389 390 391 392 393 394 395 396 397 398 399 400 401

 fail:
  grub_free (data);

  return grub_errno;
}

static grub_ssize_t
grub_romfs_read (grub_file_t file, char *buf, grub_size_t len)
{
  struct grub_fshelp_node *data = file->data;

  /* XXX: The file is stored in as a single extent.  */
  data->data->disk->read_hook = file->read_hook;
402
  data->data->disk->read_hook_data = file->read_hook_data;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  grub_disk_read (data->data->disk,
		  (data->data_addr + file->offset) >> GRUB_DISK_SECTOR_BITS,
		  (data->data_addr + file->offset) & (GRUB_DISK_SECTOR_SIZE - 1),		  
		  len, buf);
  data->data->disk->read_hook = NULL;

  if (grub_errno)
    return -1;

  return len;
}

static grub_err_t
grub_romfs_close (grub_file_t file)
{
418 419 420 421
  struct grub_fshelp_node *data = file->data;

  grub_free (data->data);
  grub_free (data);
422 423 424 425

  return GRUB_ERR_NONE;
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
static grub_err_t
grub_romfs_label (grub_device_t device, char **label)
{
  struct grub_romfs_data *data;
  grub_err_t err;

  *label = NULL;

  data = grub_romfs_mount (device);
  if (!data)
    return grub_errno;
  *label = grub_malloc (data->first_file + 1
			- sizeof (struct grub_romfs_superblock));
  if (!*label)
    {
      grub_free (data);
      return grub_errno;
    }
  err = grub_disk_read (device->disk, 0, sizeof (struct grub_romfs_superblock),
			data->first_file
			- sizeof (struct grub_romfs_superblock),
			*label);
  if (err)
    {
      grub_free (data);
      grub_free (*label);
      *label = NULL;
      return err;
    }
  (*label)[data->first_file - sizeof (struct grub_romfs_superblock)] = 0;
456
  grub_free (data);
457 458 459 460
  return GRUB_ERR_NONE;
}


461 462 463 464 465 466 467
static struct grub_fs grub_romfs_fs =
  {
    .name = "romfs",
    .dir = grub_romfs_dir,
    .open = grub_romfs_open,
    .read = grub_romfs_read,
    .close = grub_romfs_close,
468
    .label = grub_romfs_label,
469 470
#ifdef GRUB_UTIL
    .reserved_first_sector = 0,
471
    .blocklist_install = 0,
472 473 474 475 476 477 478 479 480 481 482 483 484
#endif
    .next = 0
  };

GRUB_MOD_INIT(romfs)
{
  grub_fs_register (&grub_romfs_fs);
}

GRUB_MOD_FINI(romfs)
{
  grub_fs_unregister (&grub_romfs_fs);
}