2008-09-28 Robert Millan <rmh@aybabtu.com>
[grub2/phcoder.git] / kern / disk.c
blobed82506fd08ccb205b26fbdf26c6f7f1da05335a
1 /*
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>
20 #include <grub/err.h>
21 #include <grub/mm.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;
34 /* Disk cache. */
35 struct grub_disk_cache
37 unsigned long dev_id;
38 unsigned long disk_id;
39 grub_disk_addr_t sector;
40 char *data;
41 int lock;
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 #if 0
50 static unsigned long grub_disk_cache_hits;
51 static unsigned long grub_disk_cache_misses;
53 void
54 grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
56 *hits = grub_disk_cache_hits;
57 *misses = grub_disk_cache_misses;
59 #endif
61 static unsigned
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);
70 static void
71 grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
72 grub_disk_addr_t sector)
74 unsigned index;
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)
84 cache->lock = 1;
85 grub_free (cache->data);
86 cache->data = 0;
87 cache->lock = 0;
91 void
92 grub_disk_cache_invalidate_all (void)
94 unsigned i;
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);
103 cache->data = 0;
108 static char *
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;
113 unsigned index;
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)
121 cache->lock = 1;
122 #if 0
123 grub_disk_cache_hits++;
124 #endif
125 return cache->data;
128 #if 0
129 grub_disk_cache_misses++;
130 #endif
132 return 0;
135 static void
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;
140 unsigned index;
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)
147 cache->lock = 0;
150 static grub_err_t
151 grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
152 grub_disk_addr_t sector, const char *data)
154 unsigned index;
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);
163 if (! cache->data)
164 return grub_errno;
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;
179 void
180 grub_disk_dev_register (grub_disk_dev_t dev)
182 dev->next = grub_disk_dev_list;
183 grub_disk_dev_list = dev;
186 void
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)
192 if (q == dev)
194 *p = q->next;
195 break;
200 grub_disk_dev_iterate (int (*hook) (const char *name))
202 grub_disk_dev_t p;
204 for (p = grub_disk_dev_list; p; p = p->next)
205 if (p->iterate && (p->iterate) (hook))
206 return 1;
208 return 0;
211 grub_disk_t
212 grub_disk_open (const char *name)
214 char *p;
215 grub_disk_t disk;
216 grub_disk_dev_t dev;
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));
223 if (! disk)
224 return 0;
226 disk->dev = 0;
227 disk->read_hook = 0;
228 disk->partition = 0;
229 disk->data = 0;
230 disk->name = grub_strdup (name);
231 if (! disk->name)
232 goto fail;
234 p = grub_strchr (name, ',');
235 if (p)
237 grub_size_t len = p - name;
239 raw = grub_malloc (len + 1);
240 if (! raw)
241 goto fail;
243 grub_memcpy (raw, name, len);
244 raw[len] = '\0';
247 for (dev = grub_disk_dev_list; dev; dev = dev->next)
249 if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
250 break;
251 else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
252 grub_errno = GRUB_ERR_NONE;
253 else
254 goto fail;
257 if (! dev)
259 grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
260 goto fail;
263 if (p && ! disk->has_partitions)
265 grub_error (GRUB_ERR_BAD_DEVICE, "no partition on this disk");
266 goto fail;
269 disk->dev = dev;
271 if (p)
273 disk->partition = grub_partition_probe (disk, p + 1);
274 if (! disk->partition)
276 grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such partition");
277 goto fail;
281 /* The cache will be invalidated about 2 seconds after a device was
282 closed. */
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;
291 fail:
293 if (raw && raw != name)
294 grub_free (raw);
296 if (grub_errno != GRUB_ERR_NONE)
298 grub_error_push ();
299 grub_dprintf ("disk", "Opening `%s' failed.\n", name);
300 grub_error_pop ();
302 grub_disk_close (disk);
303 return 0;
306 return disk;
309 void
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);
322 grub_free (disk);
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. */
329 static grub_err_t
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;
336 if (disk->partition)
338 grub_disk_addr_t start;
339 grub_uint64_t len;
341 start = grub_partition_get_start (disk->partition);
342 len = grub_partition_get_len (disk->partition);
344 if (*sector >= len
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");
349 *sector += start;
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. */
361 grub_err_t
362 grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
363 grub_off_t offset, grub_size_t size, char *buf)
365 char *tmp_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, &sector, &offset, size) != GRUB_ERR_NONE)
373 grub_error_push ();
374 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
375 (unsigned long long) sector, grub_errmsg);
376 grub_error_pop ();
377 return grub_errno;
380 real_offset = offset;
382 /* Allocate a temporary buffer. */
383 tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
384 if (! tmp_buf)
385 return grub_errno;
387 /* Until SIZE is zero... */
388 while (size)
390 char *data;
391 grub_disk_addr_t start_sector;
392 grub_size_t len;
393 grub_size_t pos;
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);
400 if (len > size)
401 len = size;
403 /* Fetch the cache. */
404 data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector);
405 if (data)
407 /* Just copy it! */
408 grub_memcpy (buf, data + pos + real_offset, len);
409 grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector);
411 else
413 /* Otherwise read data from the disk actually. */
414 if ((disk->dev->read) (disk, start_sector,
415 GRUB_DISK_CACHE_SIZE, tmp_buf)
416 != GRUB_ERR_NONE)
418 /* Uggh... Failed. Instead, just read necessary data. */
419 unsigned num;
420 char *p;
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);
428 if (!p)
429 goto finish;
431 tmp_buf = p;
433 if ((disk->dev->read) (disk, sector, num, tmp_buf))
435 grub_error_push ();
436 grub_dprintf ("disk", "%s read failed\n", disk->name);
437 grub_error_pop ();
438 goto finish;
441 grub_memcpy (buf, tmp_buf + real_offset, size);
443 /* Call the read hook, if any. */
444 if (disk->read_hook)
445 while (size)
447 (disk->read_hook) (sector, real_offset,
448 ((size > GRUB_DISK_SECTOR_SIZE)
449 ? GRUB_DISK_SECTOR_SIZE
450 : size));
451 sector++;
452 size -= GRUB_DISK_SECTOR_SIZE - real_offset;
453 real_offset = 0;
456 /* This must be the end. */
457 goto finish;
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. */
467 if (disk->read_hook)
469 grub_disk_addr_t s = sector;
470 grub_size_t l = len;
472 while (l)
474 (disk->read_hook) (s, real_offset,
475 ((l > GRUB_DISK_SECTOR_SIZE)
476 ? GRUB_DISK_SECTOR_SIZE
477 : l));
479 if (l < GRUB_DISK_SECTOR_SIZE - real_offset)
480 break;
482 s++;
483 l -= GRUB_DISK_SECTOR_SIZE - real_offset;
484 real_offset = 0;
488 sector = start_sector + GRUB_DISK_CACHE_SIZE;
489 buf += len;
490 size -= len;
491 real_offset = 0;
494 finish:
496 grub_free (tmp_buf);
498 return grub_errno;
501 grub_err_t
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, &sector, &offset, size) != GRUB_ERR_NONE)
510 return -1;
512 real_offset = offset;
514 while (size)
516 if (real_offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
518 char tmp_buf[GRUB_DISK_SECTOR_SIZE];
519 grub_size_t len;
521 if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
522 != GRUB_ERR_NONE)
523 goto finish;
525 len = GRUB_DISK_SECTOR_SIZE - real_offset;
526 if (len > size)
527 len = size;
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)
534 goto finish;
536 sector++;
537 buf += len;
538 size -= len;
539 real_offset = 0;
541 else
543 grub_size_t len;
544 grub_size_t n;
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)
550 goto finish;
552 while (n--)
553 grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
555 buf += len;
556 size -= len;
560 finish:
562 return grub_errno;
565 grub_uint64_t
566 grub_disk_get_size (grub_disk_t disk)
568 if (disk->partition)
569 return grub_partition_get_len (disk->partition);
570 else
571 return disk->total_sectors;