2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010 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>
27 #include <grub/i18n.h>
28 #include <grub/ventoy.h>
30 #define GRUB_CACHE_TIMEOUT 2
32 /* The last time the disk was used. */
33 static grub_uint64_t grub_last_time
= 0;
35 struct grub_disk_cache grub_disk_cache_table
[GRUB_DISK_CACHE_NUM
];
37 void (*grub_disk_firmware_fini
) (void);
38 int grub_disk_firmware_is_tainted
;
41 static unsigned long grub_disk_cache_hits
;
42 static unsigned long grub_disk_cache_misses
;
45 grub_disk_cache_get_performance (unsigned long *hits
, unsigned long *misses
)
47 *hits
= grub_disk_cache_hits
;
48 *misses
= grub_disk_cache_misses
;
52 grub_err_t (*grub_disk_write_weak
) (grub_disk_t disk
,
53 grub_disk_addr_t sector
,
57 #include "disk_common.c"
60 grub_disk_cache_invalidate_all (void)
64 for (i
= 0; i
< GRUB_DISK_CACHE_NUM
; i
++)
66 struct grub_disk_cache
*cache
= grub_disk_cache_table
+ i
;
68 if (cache
->data
&& ! cache
->lock
)
70 grub_free (cache
->data
);
77 grub_disk_cache_fetch (unsigned long dev_id
, unsigned long disk_id
,
78 grub_disk_addr_t sector
)
80 struct grub_disk_cache
*cache
;
83 cache_index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
84 cache
= grub_disk_cache_table
+ cache_index
;
86 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
87 && cache
->sector
== sector
)
91 grub_disk_cache_hits
++;
97 grub_disk_cache_misses
++;
104 grub_disk_cache_unlock (unsigned long dev_id
, unsigned long disk_id
,
105 grub_disk_addr_t sector
)
107 struct grub_disk_cache
*cache
;
108 unsigned cache_index
;
110 cache_index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
111 cache
= grub_disk_cache_table
+ cache_index
;
113 if (cache
->dev_id
== dev_id
&& cache
->disk_id
== disk_id
114 && cache
->sector
== sector
)
119 grub_disk_cache_store (unsigned long dev_id
, unsigned long disk_id
,
120 grub_disk_addr_t sector
, const char *data
)
122 unsigned cache_index
;
123 struct grub_disk_cache
*cache
;
125 cache_index
= grub_disk_cache_get_index (dev_id
, disk_id
, sector
);
126 cache
= grub_disk_cache_table
+ cache_index
;
129 grub_free (cache
->data
);
133 cache
->data
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
137 grub_memcpy (cache
->data
, data
,
138 GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
139 cache
->dev_id
= dev_id
;
140 cache
->disk_id
= disk_id
;
141 cache
->sector
= sector
;
143 return GRUB_ERR_NONE
;
148 grub_disk_dev_t grub_disk_dev_list
;
151 grub_disk_dev_register (grub_disk_dev_t dev
)
153 dev
->next
= grub_disk_dev_list
;
154 grub_disk_dev_list
= dev
;
158 grub_disk_dev_unregister (grub_disk_dev_t dev
)
160 grub_disk_dev_t
*p
, q
;
162 for (p
= &grub_disk_dev_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
170 /* Return the location of the first ',', if any, which is not
173 find_part_sep (const char *name
)
175 const char *p
= name
;
178 while ((c
= *p
++) != '\0')
180 if (c
== '\\' && *p
== ',')
189 grub_disk_open (const char *name
)
194 char *raw
= (char *) name
;
195 grub_uint64_t current_time
;
197 grub_dprintf ("disk", "Opening `%s'...\n", name
);
199 disk
= (grub_disk_t
) grub_zalloc (sizeof (*disk
));
202 disk
->log_sector_size
= GRUB_DISK_SECTOR_BITS
;
203 /* Default 1MiB of maximum agglomerate. */
204 disk
->max_agglomerate
= 1048576 >> (GRUB_DISK_SECTOR_BITS
205 + GRUB_DISK_CACHE_BITS
);
207 p
= find_part_sep (name
);
210 grub_size_t len
= p
- name
;
212 raw
= grub_malloc (len
+ 1);
216 grub_memcpy (raw
, name
, len
);
218 disk
->name
= grub_strdup (raw
);
221 disk
->name
= grub_strdup (name
);
225 for (dev
= grub_disk_dev_list
; dev
; dev
= dev
->next
)
227 if ((dev
->disk_open
) (raw
, disk
) == GRUB_ERR_NONE
)
229 else if (grub_errno
== GRUB_ERR_UNKNOWN_DEVICE
)
230 grub_errno
= GRUB_ERR_NONE
;
237 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, N_("disk `%s' not found"),
241 if (disk
->log_sector_size
> GRUB_DISK_CACHE_BITS
+ GRUB_DISK_SECTOR_BITS
242 || disk
->log_sector_size
< GRUB_DISK_SECTOR_BITS
)
244 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
245 "sector sizes of %d bytes aren't supported yet",
246 (1 << disk
->log_sector_size
));
254 disk
->partition
= grub_partition_probe (disk
, p
+ 1);
255 if (! disk
->partition
)
257 /* TRANSLATORS: It means that the specified partition e.g.
258 hd0,msdos1=/dev/sda1 doesn't exist. */
259 grub_error (GRUB_ERR_UNKNOWN_DEVICE
, N_("no such partition"));
264 /* The cache will be invalidated about 2 seconds after a device was
266 current_time
= grub_get_time_ms ();
268 if (current_time
> (grub_last_time
269 + GRUB_CACHE_TIMEOUT
* 1000))
270 grub_disk_cache_invalidate_all ();
272 grub_last_time
= current_time
;
276 if (raw
&& raw
!= name
)
279 if (grub_errno
!= GRUB_ERR_NONE
)
282 grub_dprintf ("disk", "Opening `%s' failed.\n", name
);
285 grub_disk_close (disk
);
293 grub_disk_close (grub_disk_t disk
)
295 grub_partition_t part
;
296 grub_dprintf ("disk", "Closing `%s'.\n", disk
->name
);
298 if (disk
->dev
&& disk
->dev
->disk_close
)
299 (disk
->dev
->disk_close
) (disk
);
301 /* Reset the timer. */
302 grub_last_time
= grub_get_time_ms ();
304 while (disk
->partition
)
306 part
= disk
->partition
->parent
;
307 grub_free (disk
->partition
);
308 disk
->partition
= part
;
310 grub_free ((void *) disk
->name
);
314 /* Small read (less than cache size and not pass across cache unit boundaries).
315 sector is already adjusted and is divisible by cache unit size.
318 grub_disk_read_small_real (grub_disk_t disk
, grub_disk_addr_t sector
,
319 grub_off_t offset
, grub_size_t size
, void *buf
)
324 /* Fetch the cache. */
325 data
= grub_disk_cache_fetch (disk
->dev
->id
, disk
->id
, sector
);
329 grub_memcpy (buf
, data
+ offset
, size
);
330 grub_disk_cache_unlock (disk
->dev
->id
, disk
->id
, sector
);
331 return GRUB_ERR_NONE
;
334 /* Allocate a temporary buffer. */
335 tmp_buf
= grub_malloc (GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
);
339 /* Otherwise read data from the disk actually. */
340 if (disk
->total_sectors
== GRUB_DISK_SIZE_UNKNOWN
341 || sector
+ GRUB_DISK_CACHE_SIZE
342 < (disk
->total_sectors
<< (disk
->log_sector_size
- GRUB_DISK_SECTOR_BITS
)))
345 err
= (disk
->dev
->disk_read
) (disk
, transform_sector (disk
, sector
),
346 1U << (GRUB_DISK_CACHE_BITS
347 + GRUB_DISK_SECTOR_BITS
348 - disk
->log_sector_size
), tmp_buf
);
351 /* Copy it and store it in the disk cache. */
352 grub_memcpy (buf
, tmp_buf
+ offset
, size
);
353 grub_disk_cache_store (disk
->dev
->id
, disk
->id
,
356 return GRUB_ERR_NONE
;
361 grub_errno
= GRUB_ERR_NONE
;
364 /* Uggh... Failed. Instead, just read necessary data. */
366 grub_disk_addr_t aligned_sector
;
368 sector
+= (offset
>> GRUB_DISK_SECTOR_BITS
);
369 offset
&= ((1 << GRUB_DISK_SECTOR_BITS
) - 1);
370 aligned_sector
= (sector
& ~((1ULL << (disk
->log_sector_size
371 - GRUB_DISK_SECTOR_BITS
))
373 offset
+= ((sector
- aligned_sector
) << GRUB_DISK_SECTOR_BITS
);
374 num
= ((size
+ offset
+ (1ULL << (disk
->log_sector_size
))
375 - 1) >> (disk
->log_sector_size
));
377 tmp_buf
= grub_malloc (num
<< disk
->log_sector_size
);
381 if ((disk
->dev
->disk_read
) (disk
, transform_sector (disk
, aligned_sector
),
385 grub_dprintf ("disk", "%s read failed\n", disk
->name
);
390 grub_memcpy (buf
, tmp_buf
+ offset
, size
);
392 return GRUB_ERR_NONE
;
397 grub_disk_read_small (grub_disk_t disk
, grub_disk_addr_t sector
,
398 grub_off_t offset
, grub_size_t size
, void *buf
)
402 err
= grub_disk_read_small_real (disk
, sector
, offset
, size
, buf
);
406 (disk
->read_hook
) (sector
+ (offset
>> GRUB_DISK_SECTOR_BITS
),
407 offset
& (GRUB_DISK_SECTOR_SIZE
- 1),
408 size
, disk
->read_hook_data
);
409 return GRUB_ERR_NONE
;
412 grub_err_t
grub_disk_blocklist_read(void *chunklist
, grub_uint64_t sector
,
413 grub_uint64_t size
, grub_uint32_t log_sector_size
)
415 ventoy_img_chunk
*last_chunk
= NULL
;
416 ventoy_img_chunk
*new_chunk
= NULL
;
417 ventoy_img_chunk_list
*chunk_list
= (ventoy_img_chunk_list
*)chunklist
;
419 if (chunk_list
->cur_chunk
== 0)
421 chunk_list
->chunk
[0].img_start_sector
= 0;
422 chunk_list
->chunk
[0].img_end_sector
= (size
>> 11) - 1;
423 chunk_list
->chunk
[0].disk_start_sector
= sector
;
424 chunk_list
->chunk
[0].disk_end_sector
= sector
+ (size
>> log_sector_size
) - 1;
425 chunk_list
->cur_chunk
= 1;
429 last_chunk
= chunk_list
->chunk
+ chunk_list
->cur_chunk
- 1;
430 if (last_chunk
->disk_end_sector
+ 1 == sector
)
432 last_chunk
->img_end_sector
+= (size
>> 11);
433 last_chunk
->disk_end_sector
+= (size
>> log_sector_size
);
437 if (chunk_list
->cur_chunk
== chunk_list
->max_chunk
)
439 new_chunk
= grub_realloc(chunk_list
->chunk
, chunk_list
->max_chunk
* 2 * sizeof(ventoy_img_chunk
));
440 if (NULL
== new_chunk
)
444 chunk_list
->chunk
= new_chunk
;
445 chunk_list
->max_chunk
*= 2;
447 /* issue: update last_chunk */
448 last_chunk
= chunk_list
->chunk
+ chunk_list
->cur_chunk
- 1;
451 new_chunk
= chunk_list
->chunk
+ chunk_list
->cur_chunk
;
452 new_chunk
->img_start_sector
= last_chunk
->img_end_sector
+ 1;
453 new_chunk
->img_end_sector
= new_chunk
->img_start_sector
+ (size
>> 11) - 1;
454 new_chunk
->disk_start_sector
= sector
;
455 new_chunk
->disk_end_sector
= sector
+ (size
>> log_sector_size
) - 1;
457 chunk_list
->cur_chunk
++;
462 /* Read data from the disk. */
464 grub_disk_read (grub_disk_t disk
, grub_disk_addr_t sector
,
465 grub_off_t offset
, grub_size_t size
, void *buf
)
467 if (disk
->read_hook
== (grub_disk_read_hook_t
)grub_disk_blocklist_read
)
469 return grub_disk_blocklist_read((ventoy_img_chunk_list
*)disk
->read_hook_data
, sector
, size
, disk
->log_sector_size
);
472 /* First of all, check if the region is within the disk. */
473 if (grub_disk_adjust_range (disk
, §or
, &offset
, size
) != GRUB_ERR_NONE
)
476 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
477 (unsigned long long) sector
, grub_errmsg
);
482 /* First read until first cache boundary. */
483 if (offset
|| (sector
& (GRUB_DISK_CACHE_SIZE
- 1)))
485 grub_disk_addr_t start_sector
;
490 start_sector
= sector
& ~((grub_disk_addr_t
) GRUB_DISK_CACHE_SIZE
- 1);
491 pos
= (sector
- start_sector
) << GRUB_DISK_SECTOR_BITS
;
492 len
= ((GRUB_DISK_SECTOR_SIZE
<< GRUB_DISK_CACHE_BITS
)
496 err
= grub_disk_read_small (disk
, start_sector
,
497 offset
+ pos
, len
, buf
);
500 buf
= (char *) buf
+ len
;
503 sector
+= (offset
>> GRUB_DISK_SECTOR_BITS
);
504 offset
&= ((1 << GRUB_DISK_SECTOR_BITS
) - 1);
507 /* Until SIZE is zero... */
508 while (size
>= (GRUB_DISK_CACHE_SIZE
<< GRUB_DISK_SECTOR_BITS
))
511 grub_disk_addr_t agglomerate
;
514 /* agglomerate read until we find a first cached entry. */
515 for (agglomerate
= 0; agglomerate
516 < (size
>> (GRUB_DISK_SECTOR_BITS
+ GRUB_DISK_CACHE_BITS
))
517 && agglomerate
< disk
->max_agglomerate
;
520 data
= grub_disk_cache_fetch (disk
->dev
->id
, disk
->id
,
521 sector
+ (agglomerate
522 << GRUB_DISK_CACHE_BITS
));
529 grub_memcpy ((char *) buf
530 + (agglomerate
<< (GRUB_DISK_CACHE_BITS
531 + GRUB_DISK_SECTOR_BITS
)),
532 data
, GRUB_DISK_CACHE_SIZE
<< GRUB_DISK_SECTOR_BITS
);
533 grub_disk_cache_unlock (disk
->dev
->id
, disk
->id
,
534 sector
+ (agglomerate
535 << GRUB_DISK_CACHE_BITS
));
542 err
= (disk
->dev
->disk_read
) (disk
, transform_sector (disk
, sector
),
543 agglomerate
<< (GRUB_DISK_CACHE_BITS
544 + GRUB_DISK_SECTOR_BITS
545 - disk
->log_sector_size
),
550 for (i
= 0; i
< agglomerate
; i
++)
551 grub_disk_cache_store (disk
->dev
->id
, disk
->id
,
552 sector
+ (i
<< GRUB_DISK_CACHE_BITS
),
554 + (i
<< (GRUB_DISK_CACHE_BITS
555 + GRUB_DISK_SECTOR_BITS
)));
559 (disk
->read_hook
) (sector
, 0, agglomerate
<< (GRUB_DISK_CACHE_BITS
+ GRUB_DISK_SECTOR_BITS
),
560 disk
->read_hook_data
);
562 sector
+= agglomerate
<< GRUB_DISK_CACHE_BITS
;
563 size
-= agglomerate
<< (GRUB_DISK_CACHE_BITS
+ GRUB_DISK_SECTOR_BITS
);
565 + (agglomerate
<< (GRUB_DISK_CACHE_BITS
+ GRUB_DISK_SECTOR_BITS
));
571 (disk
->read_hook
) (sector
, 0, (GRUB_DISK_CACHE_SIZE
<< GRUB_DISK_SECTOR_BITS
),
572 disk
->read_hook_data
);
573 sector
+= GRUB_DISK_CACHE_SIZE
;
574 buf
= (char *) buf
+ (GRUB_DISK_CACHE_SIZE
<< GRUB_DISK_SECTOR_BITS
);
575 size
-= (GRUB_DISK_CACHE_SIZE
<< GRUB_DISK_SECTOR_BITS
);
579 /* And now read the last part. */
583 err
= grub_disk_read_small (disk
, sector
, 0, size
, buf
);
592 grub_disk_get_size (grub_disk_t disk
)
595 return grub_partition_get_len (disk
->partition
);
596 else if (disk
->total_sectors
!= GRUB_DISK_SIZE_UNKNOWN
)
597 return disk
->total_sectors
<< (disk
->log_sector_size
- GRUB_DISK_SECTOR_BITS
);
599 return GRUB_DISK_SIZE_UNKNOWN
;