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
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
;
50 static unsigned long grub_disk_cache_hits
;
51 static unsigned long grub_disk_cache_misses
;
54 grub_disk_cache_get_performance (unsigned long *hits
, unsigned long *misses
)
56 *hits
= grub_disk_cache_hits
;
57 *misses
= grub_disk_cache_misses
;
62 grub_disk_cache_get_index (unsigned long dev_id
, unsigned long disk_id
,
63 grub_disk_addr_t sector
)
65 return ((dev_id
* 524287UL + disk_id
* 2606459UL
66 + ((unsigned) (sector
>> GRUB_DISK_CACHE_BITS
)))
67 % GRUB_DISK_CACHE_NUM
);
71 grub_disk_cache_invalidate (unsigned long dev_id
, unsigned long disk_id
,
72 grub_disk_addr_t sector
)
75 struct grub_disk_cache
*cache
;
77 sector
&= ~(GRUB_DISK_CACHE_SIZE
- 1);
78 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
79 cache
= grub_disk_cache_table
+ index
;
81 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
82 && cache
->sector
== sector
&& cache
->data
)
85 grub_free (cache
->data
);
92 grub_disk_cache_invalidate_all (void)
96 for (i
= 0; i
< GRUB_DISK_CACHE_NUM
; i
++)
98 struct grub_disk_cache
*cache
= grub_disk_cache_table
+ i
;
100 if (cache
->data
&& ! cache
->lock
)
102 grub_free (cache
->data
);
109 grub_disk_cache_fetch (unsigned long dev_id
, unsigned long disk_id
,
110 grub_disk_addr_t sector
)
112 struct grub_disk_cache
*cache
;
115 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
116 cache
= grub_disk_cache_table
+ index
;
118 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
119 && cache
->sector
== sector
)
123 grub_disk_cache_hits
++;
129 grub_disk_cache_misses
++;
136 grub_disk_cache_unlock (unsigned long dev_id
, unsigned long disk_id
,
137 grub_disk_addr_t sector
)
139 struct grub_disk_cache
*cache
;
142 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
143 cache
= grub_disk_cache_table
+ index
;
145 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
146 && cache
->sector
== sector
)
151 grub_disk_cache_store (unsigned long dev_id
, unsigned long disk_id
,
152 grub_disk_addr_t sector
, const char *data
)
155 struct grub_disk_cache
*cache
;
157 grub_disk_cache_invalidate (dev_id
, disk_id
, sector
);
159 index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
160 cache
= grub_disk_cache_table
+ index
;
162 cache
->data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
166 grub_memcpy (cache
->data
, data
,
167 GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
168 cache
->dev_id
= dev_id
;
169 cache
->disk_id
= disk_id
;
170 cache
->sector
= sector
;
172 return GRUB_ERR_NONE
;
177 static grub_disk_dev_t grub_disk_dev_list
;
180 grub_disk_dev_register (grub_disk_dev_t dev
)
182 dev
->next
= grub_disk_dev_list
;
183 grub_disk_dev_list
= dev
;
187 grub_disk_dev_unregister (grub_disk_dev_t dev
)
189 grub_disk_dev_t
*p
, q
;
191 for (p
= &grub_disk_dev_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
200 grub_disk_dev_iterate (int (*hook
) (const char *name
))
204 for (p
= grub_disk_dev_list
; p
; p
= p
->next
)
205 if (p
->iterate
&& (p
->iterate
) (hook
))
212 grub_disk_open (const char *name
)
217 char *raw
= (char *) name
;
218 grub_uint64_t current_time
;
220 grub_dprintf ("disk", "Opening `%s'...\n", name
);
222 disk
= (grub_disk_t
) grub_malloc (sizeof (*disk
));
230 disk
->name
= grub_strdup (name
);
234 p
= grub_strchr (name
, ',');
237 grub_size_t len
= p
- name
;
239 raw
= grub_malloc (len
+ 1);
243 grub_memcpy (raw
, name
, len
);
247 for (dev
= grub_disk_dev_list
; dev
; dev
= dev
->next
)
249 if ((dev
->open
) (raw
, disk
) == GRUB_ERR_NONE
)
251 else if (grub_errno
== GRUB_ERR_UNKNOWN_DEVICE
)
252 grub_errno
= GRUB_ERR_NONE
;
259 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "no such disk");
263 if (p
&& ! disk
->has_partitions
)
265 grub_error (GRUB_ERR_BAD_DEVICE
, "no partition on this disk");
273 disk
->partition
= grub_partition_probe (disk
, p
+ 1);
274 if (! disk
->partition
)
276 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "no such partition");
281 /* The cache will be invalidated about 2 seconds after a device was
283 current_time
= grub_get_time_ms ();
285 if (current_time
> (grub_last_time
286 + GRUB_CACHE_TIMEOUT
* 1000))
287 grub_disk_cache_invalidate_all ();
289 grub_last_time
= current_time
;
293 if (raw
&& raw
!= name
)
296 if (grub_errno
!= GRUB_ERR_NONE
)
299 grub_dprintf ("disk", "Opening `%s' failed.\n", name
);
302 grub_disk_close (disk
);
310 grub_disk_close (grub_disk_t disk
)
312 grub_dprintf ("disk", "Closing `%s'.\n", disk
->name
);
314 if (disk
->dev
&& disk
->dev
->close
)
315 (disk
->dev
->close
) (disk
);
317 /* Reset the timer. */
318 grub_last_time
= grub_get_time_ms ();
320 grub_free (disk
->partition
);
321 grub_free ((void *) disk
->name
);
325 /* This function performs three tasks:
326 - Make sectors disk relative from partition relative.
327 - Normalize offset to be less than the sector size.
328 - Verify that the range is inside the partition. */
330 grub_disk_adjust_range (grub_disk_t disk
, grub_disk_addr_t
*sector
,
331 grub_off_t
*offset
, grub_size_t size
)
333 *sector
+= *offset
>> GRUB_DISK_SECTOR_BITS
;
334 *offset
&= GRUB_DISK_SECTOR_SIZE
- 1;
338 grub_disk_addr_t start
;
341 start
= grub_partition_get_start (disk
->partition
);
342 len
= grub_partition_get_len (disk
->partition
);
345 || len
- *sector
< ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
346 >> GRUB_DISK_SECTOR_BITS
))
347 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of partition");
352 if (disk
->total_sectors
<= *sector
353 || ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
354 >> GRUB_DISK_SECTOR_BITS
) > disk
->total_sectors
- *sector
)
355 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of disk");
357 return GRUB_ERR_NONE
;
360 /* Read data from the disk. */
362 grub_disk_read (grub_disk_t disk
, grub_disk_addr_t sector
,
363 grub_off_t offset
, grub_size_t size
, char *buf
)
366 unsigned real_offset
;
368 grub_dprintf ("disk", "Reading `%s'...\n", disk
->name
);
370 /* First of all, check if the region is within the disk. */
371 if (grub_disk_adjust_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
374 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
375 (unsigned long long) sector
, grub_errmsg
);
380 real_offset
= offset
;
382 /* Allocate a temporary buffer. */
383 tmp_buf
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
387 /* Until SIZE is zero... */
391 grub_disk_addr_t start_sector
;
395 /* For reading bulk data. */
396 start_sector
= sector
& ~(GRUB_DISK_CACHE_SIZE
- 1);
397 pos
= (sector
- start_sector
) << GRUB_DISK_SECTOR_BITS
;
398 len
= ((GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
)
399 - pos
- real_offset
);
403 /* Fetch the cache. */
404 data
= grub_disk_cache_fetch (disk
->dev
->id
, disk
->id
, start_sector
);
408 grub_memcpy (buf
, data
+ pos
+ real_offset
, len
);
409 grub_disk_cache_unlock (disk
->dev
->id
, disk
->id
, start_sector
);
413 /* Otherwise read data from the disk actually. */
414 if ((disk
->dev
->read
) (disk
, start_sector
,
415 GRUB_DISK_CACHE_SIZE
, tmp_buf
)
418 /* Uggh... Failed. Instead, just read necessary data. */
422 grub_errno
= GRUB_ERR_NONE
;
424 num
= ((size
+ GRUB_DISK_SECTOR_SIZE
- 1)
425 >> GRUB_DISK_SECTOR_BITS
);
427 p
= grub_realloc (tmp_buf
, num
<< GRUB_DISK_SECTOR_BITS
);
433 if ((disk
->dev
->read
) (disk
, sector
, num
, tmp_buf
))
436 grub_dprintf ("disk", "%s read failed\n", disk
->name
);
441 grub_memcpy (buf
, tmp_buf
+ real_offset
, size
);
443 /* Call the read hook, if any. */
447 (disk
->read_hook
) (sector
, real_offset
,
448 ((size
> GRUB_DISK_SECTOR_SIZE
)
449 ? GRUB_DISK_SECTOR_SIZE
452 size
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
456 /* This must be the end. */
460 /* Copy it and store it in the disk cache. */
461 grub_memcpy (buf
, tmp_buf
+ pos
+ real_offset
, len
);
462 grub_disk_cache_store (disk
->dev
->id
, disk
->id
,
463 start_sector
, tmp_buf
);
466 /* Call the read hook, if any. */
469 grub_disk_addr_t s
= sector
;
474 (disk
->read_hook
) (s
, real_offset
,
475 ((l
> GRUB_DISK_SECTOR_SIZE
)
476 ? GRUB_DISK_SECTOR_SIZE
479 if (l
< GRUB_DISK_SECTOR_SIZE
- real_offset
)
483 l
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
488 sector
= start_sector
+ GRUB_DISK_CACHE_SIZE
;
502 grub_disk_write (grub_disk_t disk
, grub_disk_addr_t sector
,
503 grub_off_t offset
, grub_size_t size
, const char *buf
)
505 unsigned real_offset
;
507 grub_dprintf ("disk", "Writing `%s'...\n", disk
->name
);
509 if (grub_disk_adjust_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
512 real_offset
= offset
;
516 if (real_offset
!= 0 || (size
< GRUB_DISK_SECTOR_SIZE
&& size
!= 0))
518 char tmp_buf
[GRUB_DISK_SECTOR_SIZE
];
521 if (grub_disk_read (disk
, sector
, 0, GRUB_DISK_SECTOR_SIZE
, tmp_buf
)
525 len
= GRUB_DISK_SECTOR_SIZE
- real_offset
;
529 grub_memcpy (tmp_buf
+ real_offset
, buf
, len
);
531 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
);
533 if ((disk
->dev
->write
) (disk
, sector
, 1, tmp_buf
) != GRUB_ERR_NONE
)
546 len
= size
& ~(GRUB_DISK_SECTOR_SIZE
- 1);
547 n
= size
>> GRUB_DISK_SECTOR_BITS
;
549 if ((disk
->dev
->write
) (disk
, sector
, n
, buf
) != GRUB_ERR_NONE
)
553 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
++);
566 grub_disk_get_size (grub_disk_t disk
)
569 return grub_partition_get_len (disk
->partition
);
571 return disk
->total_sectors
;