~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to partmap/apple.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* apple.c - Read macintosh partition tables.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
 
5
 *
 
6
 *  This program 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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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 this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/disk.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/partition.h>
 
25
 
 
26
#define GRUB_APPLE_PART_MAGIC   0x504D
 
27
 
 
28
struct grub_apple_part
 
29
{
 
30
  /* The magic number to idenify this as a partition, it should have
 
31
     the value `0x504D'.  */
 
32
  grub_uint16_t magic;
 
33
 
 
34
  /* Reserved.  */
 
35
  grub_uint16_t reserved;
 
36
 
 
37
  /* The size of the partition map in blocks.  */
 
38
  grub_uint32_t partmap_size;
 
39
 
 
40
  /* The first physical block of the partition.  */
 
41
  grub_uint32_t first_phys_block;
 
42
 
 
43
  /* The amount of blocks.  */
 
44
  grub_uint32_t blockcnt;
 
45
 
 
46
  /* The partition name.  */
 
47
  char partname[32];
 
48
 
 
49
  /* The partition type.  */
 
50
  char parttype[32];
 
51
 
 
52
  /* The first datablock of the partition.  */
 
53
  grub_uint32_t datablocks_first;
 
54
 
 
55
  /* The amount datablocks.  */
 
56
  grub_uint32_t datablocks_count;
 
57
 
 
58
  /* The status of the partition. (???)  */
 
59
  grub_uint32_t status;
 
60
 
 
61
  /* The first block on which the bootcode can be found.  */
 
62
  grub_uint32_t bootcode_pos;
 
63
 
 
64
  /* The size of the bootcode in bytes.  */
 
65
  grub_uint32_t bootcode_size;
 
66
 
 
67
  /* The load address of the bootcode.  */
 
68
  grub_uint32_t bootcode_loadaddr;
 
69
 
 
70
  /* Reserved.  */
 
71
  grub_uint32_t reserved2;
 
72
  
 
73
  /* The entrypoint of the bootcode.  */
 
74
  grub_uint32_t bootcode_entrypoint;
 
75
 
 
76
  /* Reserved.  */
 
77
  grub_uint32_t reserved3;
 
78
 
 
79
  /* A checksum of the bootcode.  */
 
80
  grub_uint32_t bootcode_checksum;
 
81
 
 
82
  /* The processor type.  */
 
83
  char processor[16];
 
84
 
 
85
  /* Padding.  */
 
86
  grub_uint16_t pad[187];
 
87
};
 
88
 
 
89
static struct grub_partition_map grub_apple_partition_map;
 
90
 
 
91
#ifndef GRUB_UTIL
 
92
static grub_dl_t my_mod;
 
93
#endif
 
94
 
 
95
 
 
96
static grub_err_t
 
97
apple_partition_map_iterate (grub_disk_t disk,
 
98
                             int (*hook) (grub_disk_t disk,
 
99
                                          const grub_partition_t partition))
 
100
{
 
101
  struct grub_partition part;
 
102
  struct grub_apple_part apart;
 
103
  struct grub_disk raw;
 
104
  int partno = 0;
 
105
  int pos = GRUB_DISK_SECTOR_SIZE;
 
106
 
 
107
  /* Enforce raw disk access.  */
 
108
  raw = *disk;
 
109
  raw.partition = 0;
 
110
 
 
111
  part.partmap = &grub_apple_partition_map;
 
112
 
 
113
  for (;;)
 
114
    {
 
115
      if (grub_disk_read (&raw, pos / GRUB_DISK_SECTOR_SIZE,
 
116
                      pos % GRUB_DISK_SECTOR_SIZE,
 
117
                          sizeof (struct grub_apple_part),  (char *) &apart))
 
118
        return grub_errno;
 
119
 
 
120
      if (apart.magic != GRUB_APPLE_PART_MAGIC)
 
121
        {
 
122
          grub_dprintf ("partition",
 
123
                        "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
 
124
                        partno, apart.magic, GRUB_APPLE_PART_MAGIC);
 
125
          break;
 
126
        }
 
127
 
 
128
      part.start = apart.first_phys_block;
 
129
      part.len = apart.blockcnt;
 
130
      part.offset = pos;
 
131
      part.index = partno;
 
132
 
 
133
      grub_dprintf ("partition",
 
134
                    "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
 
135
                    partno, apart.partname, apart.parttype,
 
136
                    apart.first_phys_block, apart.blockcnt);
 
137
 
 
138
      if (hook (disk, &part))
 
139
        return grub_errno;
 
140
 
 
141
      if (apart.first_phys_block == GRUB_DISK_SECTOR_SIZE * 2)
 
142
        return 0;
 
143
 
 
144
      pos += sizeof (struct grub_apple_part);
 
145
      partno++;
 
146
    }
 
147
 
 
148
  if (pos == GRUB_DISK_SECTOR_SIZE)
 
149
    return grub_error (GRUB_ERR_BAD_PART_TABLE,
 
150
                       "Apple partition map not found.");
 
151
 
 
152
  return 0;
 
153
}
 
154
 
 
155
 
 
156
static grub_partition_t
 
157
apple_partition_map_probe (grub_disk_t disk, const char *str)
 
158
{
 
159
  grub_partition_t p = 0;
 
160
  int partnum = 0;
 
161
  char *s = (char *) str;
 
162
 
 
163
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
 
164
  
 
165
  int find_func (grub_disk_t d __attribute__ ((unused)),
 
166
                 const grub_partition_t partition)
 
167
    {
 
168
      if (partnum == partition->index)
 
169
        {
 
170
          p = (grub_partition_t) grub_malloc (sizeof (*p));
 
171
          if (! p)
 
172
            return 1;
 
173
          
 
174
          grub_memcpy (p, partition, sizeof (*p));
 
175
          return 1;
 
176
        }
 
177
      
 
178
      return 0;
 
179
    }
 
180
  
 
181
  /* Get the partition number.  */
 
182
  partnum = grub_strtoul (s, 0, 10);
 
183
  if (grub_errno)
 
184
    {
 
185
      grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
 
186
      return 0;
 
187
    }
 
188
  
 
189
  if (apple_partition_map_iterate (disk, find_func))
 
190
    goto fail;
 
191
 
 
192
  return p;
 
193
 
 
194
 fail:
 
195
  grub_free (p);
 
196
  return 0;
 
197
}
 
198
 
 
199
 
 
200
static char *
 
201
apple_partition_map_get_name (const grub_partition_t p)
 
202
{
 
203
  char *name;
 
204
 
 
205
  name = grub_malloc (13);
 
206
  if (! name)
 
207
    return 0;
 
208
 
 
209
  grub_sprintf (name, "%d", p->index);
 
210
  return name;
 
211
}
 
212
 
 
213
 
 
214
/* Partition map type.  */
 
215
static struct grub_partition_map grub_apple_partition_map =
 
216
  {
 
217
    .name = "apple_partition_map",
 
218
    .iterate = apple_partition_map_iterate,
 
219
    .probe = apple_partition_map_probe,
 
220
    .get_name = apple_partition_map_get_name
 
221
  };
 
222
 
 
223
GRUB_MOD_INIT(apple_partition_map)
 
224
{
 
225
  grub_partition_map_register (&grub_apple_partition_map);
 
226
#ifndef GRUB_UTIL
 
227
  my_mod = mod;
 
228
#endif
 
229
}
 
230
 
 
231
GRUB_MOD_FINI(apple_partition_map)
 
232
{
 
233
  grub_partition_map_unregister (&grub_apple_partition_map);
 
234
}
 
235