~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to disk/memdisk.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-08 11:39:26 UTC
  • mfrom: (17.6.26 experimental)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 104.
  • Revision ID: james.westby@ubuntu.com-20110208113926-clfs90haboyk9zip
Tags: 1.99~rc1-2
* Merge 1.98+20100804-13 and 1.98+20100804-14, updating translations:
  - Kazakh (Baurzhan Muftakhidinov / Timur Birsh).
* mkconfig_skip_dmcrypt.patch: Refer to GRUB_PRELOAD_MODULES rather than
  suggesting people write a /etc/grub.d/01_modules script (thanks, Jordan
  Uggla).
* Handle empty dir passed to grub_find_root_device_from_mountinfo; fixes
  grub-mkrelpath on btrfs subvolumes (LP: #712029).
* Add rootflags=subvol=<name> if / is on a btrfs subvolume (LP: #712029).
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* memdisk.c - Access embedded memory disk.  */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2007,2008  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB is free software: you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation, either version 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/disk.h>
21
 
#include <grub/dl.h>
22
 
#include <grub/kernel.h>
23
 
#include <grub/misc.h>
24
 
#include <grub/mm.h>
25
 
#include <grub/types.h>
26
 
 
27
 
static char *memdisk_addr;
28
 
static grub_off_t memdisk_size = 0;
29
 
 
30
 
static int
31
 
grub_memdisk_iterate (int (*hook) (const char *name))
32
 
{
33
 
  return hook ("memdisk");
34
 
}
35
 
 
36
 
static grub_err_t
37
 
grub_memdisk_open (const char *name, grub_disk_t disk)
38
 
{
39
 
  if (grub_strcmp (name, "memdisk"))
40
 
      return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a memdisk");
41
 
 
42
 
  disk->total_sectors = memdisk_size / GRUB_DISK_SECTOR_SIZE;
43
 
  disk->id = (unsigned long) "mdsk";
44
 
  disk->has_partitions = 0;
45
 
 
46
 
  return GRUB_ERR_NONE;
47
 
}
48
 
 
49
 
static void
50
 
grub_memdisk_close (grub_disk_t disk __attribute((unused)))
51
 
{
52
 
}
53
 
 
54
 
static grub_err_t
55
 
grub_memdisk_read (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
56
 
                    grub_size_t size, char *buf)
57
 
{
58
 
  grub_memcpy (buf, memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), size << GRUB_DISK_SECTOR_BITS);
59
 
  return 0;
60
 
}
61
 
 
62
 
static grub_err_t
63
 
grub_memdisk_write (grub_disk_t disk __attribute((unused)), grub_disk_addr_t sector,
64
 
                     grub_size_t size, const char *buf)
65
 
{
66
 
  grub_memcpy (memdisk_addr + (sector << GRUB_DISK_SECTOR_BITS), buf, size << GRUB_DISK_SECTOR_BITS);
67
 
  return 0;
68
 
}
69
 
 
70
 
static struct grub_disk_dev grub_memdisk_dev =
71
 
  {
72
 
    .name = "memdisk",
73
 
    .id = GRUB_DISK_DEVICE_MEMDISK_ID,
74
 
    .iterate = grub_memdisk_iterate,
75
 
    .open = grub_memdisk_open,
76
 
    .close = grub_memdisk_close,
77
 
    .read = grub_memdisk_read,
78
 
    .write = grub_memdisk_write,
79
 
    .next = 0
80
 
  };
81
 
 
82
 
GRUB_MOD_INIT(memdisk)
83
 
{
84
 
  auto int hook (struct grub_module_header *);
85
 
  int hook (struct grub_module_header *header)
86
 
    {
87
 
      if (header->type == OBJ_TYPE_MEMDISK)
88
 
        {
89
 
          char *memdisk_orig_addr;
90
 
          memdisk_orig_addr = (char *) header + sizeof (struct grub_module_header);
91
 
 
92
 
          grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
93
 
 
94
 
          memdisk_size = header->size - sizeof (struct grub_module_header);
95
 
          memdisk_addr = grub_malloc (memdisk_size);
96
 
 
97
 
          grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
98
 
          grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);
99
 
 
100
 
          grub_disk_dev_register (&grub_memdisk_dev);
101
 
          return 1;
102
 
        }
103
 
 
104
 
      return 0;
105
 
    }
106
 
 
107
 
  grub_module_iterate (hook);
108
 
}
109
 
 
110
 
GRUB_MOD_FINI(memdisk)
111
 
{
112
 
  if (! memdisk_size)
113
 
    return;
114
 
  grub_free (memdisk_addr);
115
 
  grub_disk_dev_unregister (&grub_memdisk_dev);
116
 
}