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

« back to all changes in this revision

Viewing changes to disk/i386/pc/biosdisk.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
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB; if not, write to the Free Software
 
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
 
 
20
#include <grub/machine/biosdisk.h>
 
21
#include <grub/machine/memory.h>
 
22
#include <grub/disk.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/types.h>
 
25
#include <grub/misc.h>
 
26
#include <grub/err.h>
 
27
#include <grub/term.h>
 
28
 
 
29
/* Drive Parameters.  */
 
30
struct grub_biosdisk_drp
 
31
{
 
32
  grub_uint16_t size;
 
33
  grub_uint16_t flags;
 
34
  grub_uint32_t cylinders;
 
35
  grub_uint32_t heads;
 
36
  grub_uint32_t sectors;
 
37
  grub_uint64_t total_sectors;
 
38
  grub_uint16_t bytes_per_sector;
 
39
  /* ver 2.0 or higher */
 
40
  grub_uint32_t EDD_configuration_parameters;
 
41
  /* ver 3.0 or higher */
 
42
  grub_uint16_t signature_dpi;
 
43
  grub_uint8_t length_dpi;
 
44
  grub_uint8_t reserved[3];
 
45
  grub_uint8_t name_of_host_bus[4];
 
46
  grub_uint8_t name_of_interface_type[8];
 
47
  grub_uint8_t interface_path[8];
 
48
  grub_uint8_t device_path[8];
 
49
  grub_uint8_t reserved2;
 
50
  grub_uint8_t checksum;
 
51
  
 
52
  /* XXX: This is necessary, because the BIOS of Thinkpad X20
 
53
     writes a garbage to the tail of drive parameters,
 
54
     regardless of a size specified in a caller.  */
 
55
  grub_uint8_t dummy[16];
 
56
} __attribute__ ((packed));
 
57
 
 
58
/* Disk Address Packet.  */
 
59
struct grub_biosdisk_dap
 
60
{
 
61
  grub_uint8_t length;
 
62
  grub_uint8_t reserved;
 
63
  grub_uint16_t blocks;
 
64
  grub_uint32_t buffer;
 
65
  grub_uint64_t block;
 
66
} __attribute__ ((packed));
 
67
 
 
68
 
 
69
static int
 
70
grub_biosdisk_get_drive (const char *name)
 
71
{
 
72
  unsigned long drive;
 
73
 
 
74
  if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd')
 
75
    goto fail;
 
76
    
 
77
  drive = grub_strtoul (name + 2, 0, 10);
 
78
  if (grub_errno != GRUB_ERR_NONE)
 
79
    goto fail;
 
80
 
 
81
  if (name[0] == 'h')
 
82
    drive += 0x80;
 
83
  
 
84
  return (int) drive ;
 
85
 
 
86
 fail:
 
87
  grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a biosdisk");
 
88
  return -1;
 
89
}
 
90
 
 
91
static int
 
92
grub_biosdisk_call_hook (int (*hook) (const char *name), int drive)
 
93
{
 
94
  char name[10];
 
95
 
 
96
  grub_sprintf (name, (drive & 0x80) ? "hd%d" : "fd%d", drive & (~0x80));
 
97
  return hook (name);
 
98
}
 
99
 
 
100
static int
 
101
grub_biosdisk_iterate (int (*hook) (const char *name))
 
102
{
 
103
  int drive;
 
104
  int num_floppies;
 
105
 
 
106
  /* For floppy disks, we can get the number safely.  */
 
107
  num_floppies = grub_biosdisk_get_num_floppies ();
 
108
  for (drive = 0; drive < num_floppies; drive++)
 
109
    if (grub_biosdisk_call_hook (hook, drive))
 
110
      return 1;
 
111
  
 
112
  /* For hard disks, attempt to read the MBR.  */
 
113
  for (drive = 0x80; drive < 0x90; drive++)
 
114
    {
 
115
      if (grub_biosdisk_rw_standard (0x02, drive, 0, 0, 1, 1,
 
116
                                     GRUB_MEMORY_MACHINE_SCRATCH_SEG) != 0)
 
117
        break;
 
118
      
 
119
      if (grub_biosdisk_call_hook (hook, drive))
 
120
        return 1;
 
121
    }
 
122
  
 
123
  return 0;
 
124
}
 
125
 
 
126
static grub_err_t
 
127
grub_biosdisk_open (const char *name, grub_disk_t disk)
 
128
{
 
129
  unsigned long total_sectors = 0;
 
130
  int drive;
 
131
  struct grub_biosdisk_data *data;
 
132
 
 
133
  drive = grub_biosdisk_get_drive (name);
 
134
  if (drive < 0)
 
135
    return grub_errno;
 
136
 
 
137
  disk->has_partitions = (drive & 0x80);
 
138
  disk->id = drive;
 
139
  
 
140
  data = (struct grub_biosdisk_data *) grub_malloc (sizeof (*data));
 
141
  if (! data)
 
142
    return grub_errno;
 
143
  
 
144
  data->drive = drive;
 
145
  data->flags = 0;
 
146
  
 
147
  if (drive & 0x80)
 
148
    {
 
149
      /* HDD */
 
150
      int version;
 
151
      
 
152
      version = grub_biosdisk_check_int13_extensions (drive);
 
153
      if (version)
 
154
        {
 
155
          struct grub_biosdisk_drp *drp
 
156
            = (struct grub_biosdisk_drp *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
 
157
 
 
158
          /* Clear out the DRP.  */
 
159
          grub_memset (drp, 0, sizeof (*drp));
 
160
          drp->size = sizeof (*drp);
 
161
          if (!grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
 
162
            {
 
163
              data->flags = GRUB_BIOSDISK_FLAG_LBA;
 
164
 
 
165
              /* FIXME: 2TB limit.  */
 
166
              if (drp->total_sectors)
 
167
                total_sectors = drp->total_sectors & ~0L;
 
168
              else
 
169
                /* Some buggy BIOSes doesn't return the total sectors
 
170
                   correctly but returns zero. So if it is zero, compute
 
171
                   it by C/H/S returned by the LBA BIOS call.  */
 
172
                total_sectors = drp->cylinders * drp->heads * drp->sectors;
 
173
            }
 
174
        }
 
175
    }
 
176
 
 
177
  if (grub_biosdisk_get_diskinfo_standard (drive,
 
178
                                           &data->cylinders,
 
179
                                           &data->heads,
 
180
                                           &data->sectors) != 0)
 
181
    {
 
182
      grub_free (data);
 
183
      return grub_error (GRUB_ERR_BAD_DEVICE, "cannot get C/H/S values");
 
184
    }
 
185
 
 
186
  if (! total_sectors)
 
187
    total_sectors = data->cylinders * data->heads * data->sectors;
 
188
 
 
189
  disk->total_sectors = total_sectors;
 
190
  disk->data = data;
 
191
  
 
192
  return GRUB_ERR_NONE;
 
193
}
 
194
 
 
195
static void
 
196
grub_biosdisk_close (grub_disk_t disk)
 
197
{
 
198
  grub_free (disk->data);
 
199
}
 
200
 
 
201
/* For readability.  */
 
202
#define GRUB_BIOSDISK_READ      0
 
203
#define GRUB_BIOSDISK_WRITE     1
 
204
 
 
205
static grub_err_t
 
206
grub_biosdisk_rw (int cmd, grub_disk_t disk,
 
207
                  unsigned long sector, unsigned long size,
 
208
                  unsigned segment)
 
209
{
 
210
  struct grub_biosdisk_data *data = disk->data;
 
211
  
 
212
  if (data->flags & GRUB_BIOSDISK_FLAG_LBA)
 
213
    {
 
214
      struct grub_biosdisk_dap *dap;
 
215
      
 
216
      dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR
 
217
                                          + (data->sectors
 
218
                                             << GRUB_DISK_SECTOR_BITS));
 
219
      dap->length = sizeof (*dap);
 
220
      dap->reserved = 0;
 
221
      dap->blocks = size;
 
222
      dap->buffer = segment << 16;      /* The format SEGMENT:ADDRESS.  */
 
223
      dap->block = sector;
 
224
 
 
225
      if (grub_biosdisk_rw_int13_extensions (cmd + 0x42, data->drive, dap))
 
226
        {
 
227
          /* Fall back to the CHS mode.  */
 
228
          data->flags &= ~GRUB_BIOSDISK_FLAG_LBA;
 
229
          disk->total_sectors = data->cylinders * data->heads * data->sectors;
 
230
          return grub_biosdisk_rw (cmd, disk, sector, size, segment);
 
231
        }
 
232
    }
 
233
  else
 
234
    {
 
235
      unsigned coff, hoff, soff;
 
236
      unsigned head;
 
237
      
 
238
      soff = sector % data->sectors + 1;
 
239
      head = sector / data->sectors;
 
240
      hoff = head % data->heads;
 
241
      coff = head / data->heads;
 
242
 
 
243
      if (coff >= data->cylinders)
 
244
        return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
 
245
 
 
246
      if (grub_biosdisk_rw_standard (cmd + 0x02, data->drive,
 
247
                                     coff, hoff, soff, size, segment))
 
248
        {
 
249
          switch (cmd)
 
250
            {
 
251
            case GRUB_BIOSDISK_READ:
 
252
              return grub_error (GRUB_ERR_READ_ERROR, "biosdisk read error");
 
253
            case GRUB_BIOSDISK_WRITE:
 
254
              return grub_error (GRUB_ERR_WRITE_ERROR, "biosdisk write error");
 
255
            }
 
256
        }
 
257
    }
 
258
 
 
259
  return GRUB_ERR_NONE;
 
260
}
 
261
 
 
262
static grub_err_t
 
263
grub_biosdisk_read (grub_disk_t disk, unsigned long sector,
 
264
                    unsigned long size, char *buf)
 
265
{
 
266
  struct grub_biosdisk_data *data = disk->data;
 
267
 
 
268
  while (size)
 
269
    {
 
270
      unsigned long len;
 
271
 
 
272
      len = data->sectors - (sector % data->sectors);
 
273
      if (len > size)
 
274
        len = size;
 
275
 
 
276
      if (grub_biosdisk_rw (GRUB_BIOSDISK_READ, disk, sector, len,
 
277
                            GRUB_MEMORY_MACHINE_SCRATCH_SEG))
 
278
        return grub_errno;
 
279
 
 
280
      grub_memcpy (buf, (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR,
 
281
                   len << GRUB_DISK_SECTOR_BITS);
 
282
      buf += len << GRUB_DISK_SECTOR_BITS;
 
283
      sector += len;
 
284
      size -= len;
 
285
    }
 
286
 
 
287
  return grub_errno;
 
288
}
 
289
 
 
290
static grub_err_t
 
291
grub_biosdisk_write (grub_disk_t disk, unsigned long sector,
 
292
                     unsigned long size, const char *buf)
 
293
{
 
294
  struct grub_biosdisk_data *data = disk->data;
 
295
 
 
296
  while (size)
 
297
    {
 
298
      unsigned long len;
 
299
 
 
300
      len = data->sectors - (sector % data->sectors);
 
301
      if (len > size)
 
302
        len = size;
 
303
 
 
304
      grub_memcpy ((void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, buf,
 
305
                   len << GRUB_DISK_SECTOR_BITS);
 
306
 
 
307
      if (grub_biosdisk_rw (GRUB_BIOSDISK_WRITE, disk, sector, len,
 
308
                            GRUB_MEMORY_MACHINE_SCRATCH_SEG))
 
309
        return grub_errno;
 
310
 
 
311
      buf += len << GRUB_DISK_SECTOR_BITS;
 
312
      sector += len;
 
313
      size -= len;
 
314
    }
 
315
 
 
316
  return grub_errno;
 
317
}
 
318
 
 
319
static struct grub_disk_dev grub_biosdisk_dev =
 
320
  {
 
321
    .name = "biosdisk",
 
322
    .id = GRUB_DISK_DEVICE_BIOSDISK_ID,
 
323
    .iterate = grub_biosdisk_iterate,
 
324
    .open = grub_biosdisk_open,
 
325
    .close = grub_biosdisk_close,
 
326
    .read = grub_biosdisk_read,
 
327
    .write = grub_biosdisk_write,
 
328
    .next = 0
 
329
  };
 
330
 
 
331
void
 
332
grub_biosdisk_init (void)
 
333
{
 
334
  grub_disk_dev_register (&grub_biosdisk_dev);
 
335
}
 
336
 
 
337
void
 
338
grub_biosdisk_fini (void)
 
339
{
 
340
  grub_disk_dev_unregister (&grub_biosdisk_dev);
 
341
}