2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2004,2006,2007,2008 Free Software Foundation, Inc.
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 3 of the License, or
8 * (at your option) any later version.
10 * GRUB 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.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/disk.h>
22 #include <grub/types.h>
23 #include <grub/partition.h>
24 #include <grub/misc.h>
25 #include <grub/time.h>
26 #include <grub/file.h>
28 #define GRUB_CACHE_TIMEOUT 2
30 /* The last time the disk was used. */
31 static grub_uint64_t grub_last_time
= 0;
35 struct grub_disk_cache
37 enum grub_disk_dev_id dev_id
;
38 unsigned long disk_id
;
39 grub_disk_addr_t sector
;
44 static struct grub_disk_cache grub_disk_cache_table
[GRUB_DISK_CACHE_NUM
];
46 void (*grub_disk_firmware_fini
) (void);
47 int grub_disk_firmware_is_tainted
;
49 grub_err_t (* grub_disk_ata_pass_through
) (grub_disk_t
,
50 struct grub_disk_ata_pass_through_parms
*);
54 static unsigned long grub_disk_cache_hits
;
55 static unsigned long grub_disk_cache_misses
;
58 grub_disk_cache_get_performance (unsigned long *hits
, unsigned long *misses
)
60 *hits
= grub_disk_cache_hits
;
61 *misses
= grub_disk_cache_misses
;
66 grub_disk_cache_get_index (unsigned long dev_id
, unsigned long disk_id
,
67 grub_disk_addr_t sector
)
69 return ((dev_id
* 524287UL + disk_id
* 2606459UL
70 + ((unsigned) (sector
>> GRUB_DISK_CACHE_BITS
)))
71 % GRUB_DISK_CACHE_NUM
);
75 grub_disk_cache_invalidate (unsigned long dev_id
, unsigned long disk_id
,
76 grub_disk_addr_t sector
)
79 struct grub_disk_cache
*cache
;
81 sector
&= ~(GRUB_DISK_CACHE_SIZE
- 1);
82 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
83 cache
= grub_disk_cache_table
+ index
;
85 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
86 && cache
->sector
== sector
&& cache
->data
)
89 grub_free (cache
->data
);
96 grub_disk_cache_invalidate_all (void)
100 for (i
= 0; i
< GRUB_DISK_CACHE_NUM
; i
++)
102 struct grub_disk_cache
*cache
= grub_disk_cache_table
+ i
;
104 if (cache
->data
&& ! cache
->lock
)
106 grub_free (cache
->data
);
113 grub_disk_cache_fetch (unsigned long dev_id
, unsigned long disk_id
,
114 grub_disk_addr_t sector
)
116 struct grub_disk_cache
*cache
;
119 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
120 cache
= grub_disk_cache_table
+ index
;
122 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
123 && cache
->sector
== sector
)
127 grub_disk_cache_hits
++;
133 grub_disk_cache_misses
++;
140 grub_disk_cache_unlock (unsigned long dev_id
, unsigned long disk_id
,
141 grub_disk_addr_t sector
)
143 struct grub_disk_cache
*cache
;
146 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
147 cache
= grub_disk_cache_table
+ index
;
149 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
150 && cache
->sector
== sector
)
155 grub_disk_cache_store (unsigned long dev_id
, unsigned long disk_id
,
156 grub_disk_addr_t sector
, const char *data
)
159 struct grub_disk_cache
*cache
;
161 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
162 cache
= grub_disk_cache_table
+ index
;
165 grub_free (cache
->data
);
169 cache
->data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
173 grub_memcpy (cache
->data
, data
,
174 GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
175 cache
->dev_id
= dev_id
;
176 cache
->disk_id
= disk_id
;
177 cache
->sector
= sector
;
179 return GRUB_ERR_NONE
;
184 static grub_disk_dev_t grub_disk_dev_list
;
187 grub_disk_dev_register (grub_disk_dev_t dev
)
189 dev
->next
= grub_disk_dev_list
;
190 grub_disk_dev_list
= dev
;
194 grub_disk_dev_unregister (grub_disk_dev_t dev
)
196 grub_disk_dev_t
*p
, q
;
198 for (p
= &grub_disk_dev_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
207 grub_disk_dev_iterate (int (*hook
) (const char *name
))
211 for (p
= grub_disk_dev_list
; p
; p
= p
->next
)
212 if (p
->iterate
&& (p
->iterate
) (hook
))
218 /* Return the location of the first ',', if any, which is not
221 find_part_sep (const char *name
)
223 const char *p
= name
;
226 while ((c
= *p
++) != '\0')
228 if (c
== '\\' && *p
== ',')
237 grub_disk_open (const char *name
)
242 char *raw
= (char *) name
;
243 grub_uint64_t current_time
;
245 grub_dprintf ("disk", "Opening `%s'...\n", name
);
247 disk
= (grub_disk_t
) grub_zalloc (sizeof (*disk
));
251 disk
->name
= grub_strdup (name
);
255 p
= find_part_sep (name
);
258 grub_size_t len
= p
- name
;
260 raw
= grub_malloc (len
+ 1);
264 grub_memcpy (raw
, name
, len
);
268 for (dev
= grub_disk_dev_list
; dev
; dev
= dev
->next
)
270 if ((dev
->open
) (raw
, disk
) == GRUB_ERR_NONE
)
272 else if (grub_errno
== GRUB_ERR_UNKNOWN_DEVICE
)
273 grub_errno
= GRUB_ERR_NONE
;
280 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "no such disk");
284 if (p
&& ! disk
->has_partitions
)
286 grub_error (GRUB_ERR_BAD_DEVICE
, "no partition on this disk");
294 disk
->partition
= grub_partition_probe (disk
, p
+ 1);
295 if (! disk
->partition
)
297 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "no such partition");
302 /* The cache will be invalidated about 2 seconds after a device was
304 current_time
= grub_get_time_ms ();
306 if (current_time
> (grub_last_time
307 + GRUB_CACHE_TIMEOUT
* 1000))
308 grub_disk_cache_invalidate_all ();
310 grub_last_time
= current_time
;
314 if (raw
&& raw
!= name
)
317 if (grub_errno
!= GRUB_ERR_NONE
)
320 grub_dprintf ("disk", "Opening `%s' failed.\n", name
);
323 grub_disk_close (disk
);
331 grub_disk_close (grub_disk_t disk
)
333 grub_dprintf ("disk", "Closing `%s'.\n", disk
->name
);
335 if (disk
->dev
&& disk
->dev
->close
)
336 (disk
->dev
->close
) (disk
);
338 /* Reset the timer. */
339 grub_last_time
= grub_get_time_ms ();
341 grub_free (disk
->partition
);
342 grub_free ((void *) disk
->name
);
346 /* This function performs three tasks:
347 - Make sectors disk relative from partition relative.
348 - Normalize offset to be less than the sector size.
349 - Verify that the range is inside the partition. */
351 grub_disk_adjust_range (grub_disk_t disk
, grub_disk_addr_t
*sector
,
352 grub_off_t
*offset
, grub_size_t size
)
354 *sector
+= *offset
>> GRUB_DISK_SECTOR_BITS
;
355 *offset
&= GRUB_DISK_SECTOR_SIZE
- 1;
359 grub_disk_addr_t start
;
362 start
= grub_partition_get_start (disk
->partition
);
363 len
= grub_partition_get_len (disk
->partition
);
366 || len
- *sector
< ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
367 >> GRUB_DISK_SECTOR_BITS
))
368 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of partition");
373 if (disk
->total_sectors
<= *sector
374 || ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
375 >> GRUB_DISK_SECTOR_BITS
) > disk
->total_sectors
- *sector
)
376 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of disk");
378 return GRUB_ERR_NONE
;
381 /* Read data from the disk. */
383 grub_disk_read (grub_disk_t disk
, grub_disk_addr_t sector
,
384 grub_off_t offset
, grub_size_t size
, void *buf
)
387 unsigned real_offset
;
389 grub_dprintf ("disk", "Reading `%s'...\n", disk
->name
);
391 /* First of all, check if the region is within the disk. */
392 if (grub_disk_adjust_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
395 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
396 (unsigned long long) sector
, grub_errmsg
);
401 real_offset
= offset
;
403 /* Allocate a temporary buffer. */
404 tmp_buf
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
408 /* Until SIZE is zero... */
412 grub_disk_addr_t start_sector
;
416 /* For reading bulk data. */
417 start_sector
= sector
& ~(GRUB_DISK_CACHE_SIZE
- 1);
418 pos
= (sector
- start_sector
) << GRUB_DISK_SECTOR_BITS
;
419 len
= ((GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
)
420 - pos
- real_offset
);
424 /* Fetch the cache. */
425 data
= grub_disk_cache_fetch (disk
->dev
->id
, disk
->id
, start_sector
);
429 grub_memcpy (buf
, data
+ pos
+ real_offset
, len
);
430 grub_disk_cache_unlock (disk
->dev
->id
, disk
->id
, start_sector
);
434 /* Otherwise read data from the disk actually. */
435 if ((disk
->dev
->read
) (disk
, start_sector
,
436 GRUB_DISK_CACHE_SIZE
, tmp_buf
)
439 /* Uggh... Failed. Instead, just read necessary data. */
443 grub_errno
= GRUB_ERR_NONE
;
445 num
= ((size
+ GRUB_DISK_SECTOR_SIZE
- 1)
446 >> GRUB_DISK_SECTOR_BITS
);
448 p
= grub_realloc (tmp_buf
, num
<< GRUB_DISK_SECTOR_BITS
);
454 if ((disk
->dev
->read
) (disk
, sector
, num
, tmp_buf
))
457 grub_dprintf ("disk", "%s read failed\n", disk
->name
);
462 grub_memcpy (buf
, tmp_buf
+ real_offset
, size
);
464 /* Call the read hook, if any. */
468 (disk
->read_hook
) (sector
, real_offset
,
469 ((size
> GRUB_DISK_SECTOR_SIZE
)
470 ? GRUB_DISK_SECTOR_SIZE
473 size
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
477 /* This must be the end. */
481 /* Copy it and store it in the disk cache. */
482 grub_memcpy (buf
, tmp_buf
+ pos
+ real_offset
, len
);
483 grub_disk_cache_store (disk
->dev
->id
, disk
->id
,
484 start_sector
, tmp_buf
);
487 /* Call the read hook, if any. */
490 grub_disk_addr_t s
= sector
;
495 (disk
->read_hook
) (s
, real_offset
,
496 ((l
> GRUB_DISK_SECTOR_SIZE
)
497 ? GRUB_DISK_SECTOR_SIZE
500 if (l
< GRUB_DISK_SECTOR_SIZE
- real_offset
)
504 l
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
509 sector
= start_sector
+ GRUB_DISK_CACHE_SIZE
;
510 buf
= (char *) buf
+ len
;
523 grub_disk_write (grub_disk_t disk
, grub_disk_addr_t sector
,
524 grub_off_t offset
, grub_size_t size
, const void *buf
)
526 unsigned real_offset
;
528 grub_dprintf ("disk", "Writing `%s'...\n", disk
->name
);
530 if (grub_disk_adjust_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
533 real_offset
= offset
;
537 if (real_offset
!= 0 || (size
< GRUB_DISK_SECTOR_SIZE
&& size
!= 0))
539 char tmp_buf
[GRUB_DISK_SECTOR_SIZE
];
541 grub_partition_t part
;
543 part
= disk
->partition
;
545 if (grub_disk_read (disk
, sector
, 0, GRUB_DISK_SECTOR_SIZE
, tmp_buf
)
548 disk
->partition
= part
;
551 disk
->partition
= part
;
553 len
= GRUB_DISK_SECTOR_SIZE
- real_offset
;
557 grub_memcpy (tmp_buf
+ real_offset
, buf
, len
);
559 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
);
561 if ((disk
->dev
->write
) (disk
, sector
, 1, tmp_buf
) != GRUB_ERR_NONE
)
565 buf
= (char *) buf
+ len
;
574 len
= size
& ~(GRUB_DISK_SECTOR_SIZE
- 1);
575 n
= size
>> GRUB_DISK_SECTOR_BITS
;
577 if ((disk
->dev
->write
) (disk
, sector
, n
, buf
) != GRUB_ERR_NONE
)
581 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
++);
583 buf
= (char *) buf
+ len
;
594 grub_disk_get_size (grub_disk_t disk
)
597 return grub_partition_get_len (disk
->partition
);
599 return disk
->total_sectors
;