~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

Viewing changes to partmap/sun.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
/* sun.c - Read SUN style partition tables.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2002, 2005 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 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 GRUB; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/partition.h>
 
22
#include <grub/disk.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/dl.h>
 
26
#include <grub/symbol.h>
 
27
#include <grub/types.h>
 
28
#include <grub/err.h>
 
29
 
 
30
#define GRUB_PARTMAP_SUN_MAGIC 0xDABE
 
31
#define GRUB_PARTMAP_SUN_MAX_PARTS 8
 
32
#define GRUB_PARTMAP_SUN_WHOLE_DISK_ID 0x05
 
33
 
 
34
struct grub_sun_partition_info
 
35
{
 
36
  grub_uint8_t spare1;
 
37
  grub_uint8_t id;
 
38
  grub_uint8_t spare2;
 
39
  grub_uint8_t flags;
 
40
} __attribute__ ((packed));
 
41
 
 
42
struct grub_sun_partition_descriptor
 
43
{
 
44
  grub_uint32_t start_cylinder;
 
45
  grub_uint32_t num_sectors;
 
46
} __attribute__ ((packed));
 
47
 
 
48
struct grub_sun_block
 
49
{
 
50
  grub_uint8_t  info[128];      /* Informative text string.  */
 
51
  grub_uint8_t  spare0[14];
 
52
  struct grub_sun_partition_info infos[8];
 
53
  grub_uint8_t  spare1[246];    /* Boot information etc.  */
 
54
  grub_uint16_t  rspeed;        /* Disk rotational speed.  */
 
55
  grub_uint16_t  pcylcount;     /* Physical cylinder count.  */
 
56
  grub_uint16_t  sparecyl;      /* extra sects per cylinder.  */
 
57
  grub_uint8_t  spare2[4];      /* More magic...  */
 
58
  grub_uint16_t  ilfact;        /* Interleave factor.  */
 
59
  grub_uint16_t  ncyl;          /* Data cylinder count.  */
 
60
  grub_uint16_t  nacyl;         /* Alt. cylinder count.  */
 
61
  grub_uint16_t  ntrks;         /* Tracks per cylinder.  */
 
62
  grub_uint16_t  nsect;         /* Sectors per track.  */
 
63
  grub_uint8_t  spare3[4];      /* Even more magic...  */
 
64
  struct grub_sun_partition_descriptor partitions[8];
 
65
  grub_uint16_t  magic;         /* Magic number.  */
 
66
  grub_uint16_t  csum;          /* Label xor'd checksum.  */
 
67
} __attribute__ ((packed));
 
68
 
 
69
static struct grub_partition_map grub_sun_partition_map;
 
70
 
 
71
#ifndef GRUB_UTIL
 
72
static grub_dl_t my_mod;
 
73
#endif
 
74
 
 
75
/* Verify checksum (true=ok).  */
 
76
static int
 
77
grub_sun_is_valid (struct grub_sun_block *label)
 
78
{
 
79
  grub_uint16_t *pos;
 
80
  grub_uint16_t sum = 0;
 
81
  for (pos = (grub_uint16_t *) label; pos < (grub_uint16_t *) (label + 1); pos++)
 
82
    sum ^= *pos;
 
83
  return !sum;
 
84
}
 
85
 
 
86
static grub_err_t
 
87
sun_partition_map_iterate (grub_disk_t disk,
 
88
                           int (*hook) (grub_disk_t disk,
 
89
                                        const grub_partition_t partition))
 
90
{
 
91
  struct grub_partition *p;
 
92
  struct grub_disk raw;
 
93
  struct grub_sun_block block;
 
94
  int partnum;
 
95
  raw = *disk;
 
96
  raw.partition = 0;
 
97
  p = (struct grub_partition *) grub_malloc (sizeof (struct grub_partition));
 
98
  if (!p)
 
99
    return grub_errno;
 
100
 
 
101
  p->offset = 0;
 
102
  p->data = 0;
 
103
  p->partmap = &grub_sun_partition_map;
 
104
  if (grub_disk_read (&raw, 0, 0, sizeof (struct grub_sun_block),
 
105
                      (char *) &block) == GRUB_ERR_NONE)
 
106
    {
 
107
      if (GRUB_PARTMAP_SUN_MAGIC != grub_be_to_cpu16 (block.magic))
 
108
        grub_error (GRUB_ERR_BAD_PART_TABLE, "not a sun partiton table");
 
109
      if (!grub_sun_is_valid (&block))
 
110
        grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
 
111
      /* Maybe another error value would be better, because partition
 
112
         table _is_ recognised but invalid.  */
 
113
      for (partnum = 0; partnum < GRUB_PARTMAP_SUN_MAX_PARTS; partnum++)
 
114
        {
 
115
          if (block.infos[partnum].id == 0 ||
 
116
              block.infos[partnum].id == GRUB_PARTMAP_SUN_WHOLE_DISK_ID)
 
117
            continue;
 
118
          p->start = grub_be_to_cpu32
 
119
            (block.partitions[partnum].start_cylinder)
 
120
            * grub_be_to_cpu16 (block.ntrks)
 
121
            * grub_be_to_cpu16 (block.nsect);
 
122
          p->len = grub_be_to_cpu32 (block.partitions[partnum].num_sectors);
 
123
          p->index = partnum;
 
124
          if (p->len)
 
125
            {
 
126
              if (hook (disk, p))
 
127
                partnum = GRUB_PARTMAP_SUN_MAX_PARTS;
 
128
            }
 
129
        }
 
130
    }
 
131
  grub_free (p);
 
132
 
 
133
  return grub_errno;
 
134
}
 
135
 
 
136
static grub_partition_t
 
137
sun_partition_map_probe (grub_disk_t disk, const char *str)
 
138
{
 
139
  grub_partition_t p = 0;
 
140
  int partnum = 0;
 
141
  char *s = (char *) str;
 
142
 
 
143
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
 
144
  
 
145
  int find_func (grub_disk_t d __attribute__ ((unused)),
 
146
                 const grub_partition_t partition)
 
147
    {
 
148
      if (partnum == partition->index)
 
149
        {
 
150
          p = (grub_partition_t) grub_malloc (sizeof (*p));
 
151
          if (p)
 
152
            grub_memcpy (p, partition, sizeof (*p));
 
153
          return 1;
 
154
        }
 
155
      return 0;
 
156
    }
 
157
 
 
158
  grub_errno = GRUB_ERR_NONE;
 
159
  partnum = grub_strtoul (s, 0, 10);
 
160
  if (grub_errno == GRUB_ERR_NONE)
 
161
    {
 
162
      if (sun_partition_map_iterate (disk, find_func))
 
163
        {
 
164
          grub_free (p);
 
165
          p = 0;
 
166
        }
 
167
    }
 
168
  else
 
169
    {
 
170
      grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
 
171
      p = 0;
 
172
    }
 
173
  return p;
 
174
}
 
175
 
 
176
static char *
 
177
sun_partition_map_get_name (const grub_partition_t p)
 
178
{
 
179
  char *name;
 
180
  name = grub_malloc (13);
 
181
  if (name)
 
182
    grub_sprintf (name, "%d", p->index);
 
183
  return name;
 
184
}
 
185
 
 
186
/* Partition map type.  */
 
187
static struct grub_partition_map grub_sun_partition_map =
 
188
  {
 
189
    .name = "sun_partition_map",
 
190
    .iterate = sun_partition_map_iterate,
 
191
    .probe = sun_partition_map_probe,
 
192
    .get_name = sun_partition_map_get_name
 
193
  };
 
194
 
 
195
GRUB_MOD_INIT(sun_partition_map)
 
196
{
 
197
  grub_partition_map_register (&grub_sun_partition_map);
 
198
#ifndef GRUB_UTIL
 
199
  my_mod = mod;
 
200
#endif
 
201
}
 
202
 
 
203
GRUB_MOD_FINI(sun_partition_map)
 
204
{
 
205
  grub_partition_map_unregister (&grub_sun_partition_map);
 
206
}
 
207