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/machine/time.h>
26 #include <grub/file.h>
28 #define GRUB_CACHE_TIMEOUT 2
30 /* The last time the disk was used. */
31 static unsigned long 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
) (hook
))
212 grub_disk_open (const char *name
)
217 char *raw
= (char *) name
;
218 unsigned long 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_rtc ();
285 if (current_time
> (grub_last_time
286 + GRUB_CACHE_TIMEOUT
* GRUB_TICKS_PER_SECOND
))
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_rtc ();
320 grub_free (disk
->partition
);
321 grub_free ((void *) disk
->name
);
326 grub_disk_check_range (grub_disk_t disk
, grub_disk_addr_t
*sector
,
327 grub_off_t
*offset
, grub_size_t size
)
329 *sector
+= *offset
>> GRUB_DISK_SECTOR_BITS
;
330 *offset
&= GRUB_DISK_SECTOR_SIZE
- 1;
334 grub_disk_addr_t start
;
337 start
= grub_partition_get_start (disk
->partition
);
338 len
= grub_partition_get_len (disk
->partition
);
341 || len
- *sector
< ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
342 >> GRUB_DISK_SECTOR_BITS
))
343 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of partition");
348 if (disk
->total_sectors
<= *sector
349 || ((*offset
+ size
+ GRUB_DISK_SECTOR_SIZE
- 1)
350 >> GRUB_DISK_SECTOR_BITS
) > disk
->total_sectors
- *sector
)
351 return grub_error (GRUB_ERR_OUT_OF_RANGE
, "out of disk");
353 return GRUB_ERR_NONE
;
356 /* Read data from the disk. */
358 grub_disk_read (grub_disk_t disk
, grub_disk_addr_t sector
,
359 grub_off_t offset
, grub_size_t size
, char *buf
)
362 unsigned real_offset
;
364 grub_dprintf ("disk", "Reading `%s'...\n", disk
->name
);
366 /* First of all, check if the region is within the disk. */
367 if (grub_disk_check_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
370 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
371 (unsigned long long) sector
, grub_errmsg
);
376 real_offset
= offset
;
378 /* Allocate a temporary buffer. */
379 tmp_buf
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
383 /* Until SIZE is zero... */
387 grub_disk_addr_t start_sector
;
391 /* For reading bulk data. */
392 start_sector
= sector
& ~(GRUB_DISK_CACHE_SIZE
- 1);
393 pos
= (sector
- start_sector
) << GRUB_DISK_SECTOR_BITS
;
394 len
= ((GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
)
395 - pos
- real_offset
);
399 /* Fetch the cache. */
400 data
= grub_disk_cache_fetch (disk
->dev
->id
, disk
->id
, start_sector
);
404 grub_memcpy (buf
, data
+ pos
+ real_offset
, len
);
405 grub_disk_cache_unlock (disk
->dev
->id
, disk
->id
, start_sector
);
409 /* Otherwise read data from the disk actually. */
410 if ((disk
->dev
->read
) (disk
, start_sector
,
411 GRUB_DISK_CACHE_SIZE
, tmp_buf
)
414 /* Uggh... Failed. Instead, just read necessary data. */
418 grub_errno
= GRUB_ERR_NONE
;
420 num
= ((size
+ GRUB_DISK_SECTOR_SIZE
- 1)
421 >> GRUB_DISK_SECTOR_BITS
);
423 p
= grub_realloc (tmp_buf
, num
<< GRUB_DISK_SECTOR_BITS
);
429 if ((disk
->dev
->read
) (disk
, sector
, num
, tmp_buf
))
432 grub_dprintf ("disk", "%s read failed\n", disk
->name
);
437 grub_memcpy (buf
, tmp_buf
+ real_offset
, size
);
439 /* Call the read hook, if any. */
443 (disk
->read_hook
) (sector
, real_offset
,
444 ((size
> GRUB_DISK_SECTOR_SIZE
)
445 ? GRUB_DISK_SECTOR_SIZE
448 size
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
452 /* This must be the end. */
456 /* Copy it and store it in the disk cache. */
457 grub_memcpy (buf
, tmp_buf
+ pos
+ real_offset
, len
);
458 grub_disk_cache_store (disk
->dev
->id
, disk
->id
,
459 start_sector
, tmp_buf
);
462 /* Call the read hook, if any. */
465 grub_disk_addr_t s
= sector
;
470 (disk
->read_hook
) (s
, real_offset
,
471 ((l
> GRUB_DISK_SECTOR_SIZE
)
472 ? GRUB_DISK_SECTOR_SIZE
475 if (l
< GRUB_DISK_SECTOR_SIZE
- real_offset
)
479 l
-= GRUB_DISK_SECTOR_SIZE
- real_offset
;
484 sector
= start_sector
+ GRUB_DISK_CACHE_SIZE
;
498 grub_disk_write (grub_disk_t disk
, grub_disk_addr_t sector
,
499 grub_off_t offset
, grub_size_t size
, const char *buf
)
501 unsigned real_offset
;
503 grub_dprintf ("disk", "Writing `%s'...\n", disk
->name
);
505 if (grub_disk_check_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
508 real_offset
= offset
;
512 if (real_offset
!= 0 || (size
< GRUB_DISK_SECTOR_SIZE
&& size
!= 0))
514 char tmp_buf
[GRUB_DISK_SECTOR_SIZE
];
517 if (grub_disk_read (disk
, sector
, 0, GRUB_DISK_SECTOR_SIZE
, tmp_buf
)
521 len
= GRUB_DISK_SECTOR_SIZE
- real_offset
;
525 grub_memcpy (tmp_buf
+ real_offset
, buf
, len
);
527 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
);
529 if ((disk
->dev
->write
) (disk
, sector
, 1, tmp_buf
) != GRUB_ERR_NONE
)
542 len
= size
& ~(GRUB_DISK_SECTOR_SIZE
- 1);
543 n
= size
>> GRUB_DISK_SECTOR_BITS
;
545 if ((disk
->dev
->write
) (disk
, sector
, n
, buf
) != GRUB_ERR_NONE
)
549 grub_disk_cache_invalidate (disk
->dev
->id
, disk
->id
, sector
++);
562 grub_disk_get_size (grub_disk_t disk
)
565 return grub_partition_get_len (disk
->partition
);
567 return disk
->total_sectors
;