2009-04-13 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / kern / disk.c
blob8a92989ade74e93cba9fdb28759415ce699eced9
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 grub_err_t (* grub_disk_ata_pass_through) (grub_disk_t,
50 struct grub_disk_ata_pass_through_parms *);
53 #if 0
54 static unsigned long grub_disk_cache_hits;
55 static unsigned long grub_disk_cache_misses;
57 void
58 grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
60 *hits = grub_disk_cache_hits;
61 *misses = grub_disk_cache_misses;
63 #endif
65 static unsigned
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);
74 static void
75 grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
76 grub_disk_addr_t sector)
78 unsigned index;
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)
88 cache->lock = 1;
89 grub_free (cache->data);
90 cache->data = 0;
91 cache->lock = 0;
95 void
96 grub_disk_cache_invalidate_all (void)
98 unsigned i;
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);
107 cache->data = 0;
112 static char *
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;
117 unsigned index;
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)
125 cache->lock = 1;
126 #if 0
127 grub_disk_cache_hits++;
128 #endif
129 return cache->data;
132 #if 0
133 grub_disk_cache_misses++;
134 #endif
136 return 0;
139 static void
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;
144 unsigned index;
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)
151 cache->lock = 0;
154 static grub_err_t
155 grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
156 grub_disk_addr_t sector, const char *data)
158 unsigned index;
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;
164 cache->lock = 1;
165 grub_free (cache->data);
166 cache->data = 0;
167 cache->lock = 0;
169 cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
170 if (! cache->data)
171 return grub_errno;
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;
186 void
187 grub_disk_dev_register (grub_disk_dev_t dev)
189 dev->next = grub_disk_dev_list;
190 grub_disk_dev_list = dev;
193 void
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)
199 if (q == dev)
201 *p = q->next;
202 break;
207 grub_disk_dev_iterate (int (*hook) (const char *name))
209 grub_disk_dev_t p;
211 for (p = grub_disk_dev_list; p; p = p->next)
212 if (p->iterate && (p->iterate) (hook))
213 return 1;
215 return 0;
218 grub_disk_t
219 grub_disk_open (const char *name)
221 char *p;
222 grub_disk_t disk;
223 grub_disk_dev_t dev;
224 char *raw = (char *) name;
225 grub_uint64_t current_time;
227 grub_dprintf ("disk", "Opening `%s'...\n", name);
229 disk = (grub_disk_t) grub_malloc (sizeof (*disk));
230 if (! disk)
231 return 0;
233 disk->dev = 0;
234 disk->read_hook = 0;
235 disk->partition = 0;
236 disk->data = 0;
237 disk->name = grub_strdup (name);
238 if (! disk->name)
239 goto fail;
241 p = grub_strchr (name, ',');
242 if (p)
244 grub_size_t len = p - name;
246 raw = grub_malloc (len + 1);
247 if (! raw)
248 goto fail;
250 grub_memcpy (raw, name, len);
251 raw[len] = '\0';
254 for (dev = grub_disk_dev_list; dev; dev = dev->next)
256 if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
257 break;
258 else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
259 grub_errno = GRUB_ERR_NONE;
260 else
261 goto fail;
264 if (! dev)
266 grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
267 goto fail;
270 if (p && ! disk->has_partitions)
272 grub_error (GRUB_ERR_BAD_DEVICE, "no partition on this disk");
273 goto fail;
276 disk->dev = dev;
278 if (p)
280 disk->partition = grub_partition_probe (disk, p + 1);
281 if (! disk->partition)
283 grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such partition");
284 goto fail;
288 /* The cache will be invalidated about 2 seconds after a device was
289 closed. */
290 current_time = grub_get_time_ms ();
292 if (current_time > (grub_last_time
293 + GRUB_CACHE_TIMEOUT * 1000))
294 grub_disk_cache_invalidate_all ();
296 grub_last_time = current_time;
298 fail:
300 if (raw && raw != name)
301 grub_free (raw);
303 if (grub_errno != GRUB_ERR_NONE)
305 grub_error_push ();
306 grub_dprintf ("disk", "Opening `%s' failed.\n", name);
307 grub_error_pop ();
309 grub_disk_close (disk);
310 return 0;
313 return disk;
316 void
317 grub_disk_close (grub_disk_t disk)
319 grub_dprintf ("disk", "Closing `%s'.\n", disk->name);
321 if (disk->dev && disk->dev->close)
322 (disk->dev->close) (disk);
324 /* Reset the timer. */
325 grub_last_time = grub_get_time_ms ();
327 grub_free (disk->partition);
328 grub_free ((void *) disk->name);
329 grub_free (disk);
332 /* This function performs three tasks:
333 - Make sectors disk relative from partition relative.
334 - Normalize offset to be less than the sector size.
335 - Verify that the range is inside the partition. */
336 static grub_err_t
337 grub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector,
338 grub_off_t *offset, grub_size_t size)
340 *sector += *offset >> GRUB_DISK_SECTOR_BITS;
341 *offset &= GRUB_DISK_SECTOR_SIZE - 1;
343 if (disk->partition)
345 grub_disk_addr_t start;
346 grub_uint64_t len;
348 start = grub_partition_get_start (disk->partition);
349 len = grub_partition_get_len (disk->partition);
351 if (*sector >= len
352 || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
353 >> GRUB_DISK_SECTOR_BITS))
354 return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition");
356 *sector += start;
359 if (disk->total_sectors <= *sector
360 || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
361 >> GRUB_DISK_SECTOR_BITS) > disk->total_sectors - *sector)
362 return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
364 return GRUB_ERR_NONE;
367 /* Read data from the disk. */
368 grub_err_t
369 grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
370 grub_off_t offset, grub_size_t size, char *buf)
372 char *tmp_buf;
373 unsigned real_offset;
375 grub_dprintf ("disk", "Reading `%s'...\n", disk->name);
377 /* First of all, check if the region is within the disk. */
378 if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
380 grub_error_push ();
381 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
382 (unsigned long long) sector, grub_errmsg);
383 grub_error_pop ();
384 return grub_errno;
387 real_offset = offset;
389 /* Allocate a temporary buffer. */
390 tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
391 if (! tmp_buf)
392 return grub_errno;
394 /* Until SIZE is zero... */
395 while (size)
397 char *data;
398 grub_disk_addr_t start_sector;
399 grub_size_t len;
400 grub_size_t pos;
402 /* For reading bulk data. */
403 start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1);
404 pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
405 len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS)
406 - pos - real_offset);
407 if (len > size)
408 len = size;
410 /* Fetch the cache. */
411 data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector);
412 if (data)
414 /* Just copy it! */
415 grub_memcpy (buf, data + pos + real_offset, len);
416 grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector);
418 else
420 /* Otherwise read data from the disk actually. */
421 if ((disk->dev->read) (disk, start_sector,
422 GRUB_DISK_CACHE_SIZE, tmp_buf)
423 != GRUB_ERR_NONE)
425 /* Uggh... Failed. Instead, just read necessary data. */
426 unsigned num;
427 char *p;
429 grub_errno = GRUB_ERR_NONE;
431 num = ((size + GRUB_DISK_SECTOR_SIZE - 1)
432 >> GRUB_DISK_SECTOR_BITS);
434 p = grub_realloc (tmp_buf, num << GRUB_DISK_SECTOR_BITS);
435 if (!p)
436 goto finish;
438 tmp_buf = p;
440 if ((disk->dev->read) (disk, sector, num, tmp_buf))
442 grub_error_push ();
443 grub_dprintf ("disk", "%s read failed\n", disk->name);
444 grub_error_pop ();
445 goto finish;
448 grub_memcpy (buf, tmp_buf + real_offset, size);
450 /* Call the read hook, if any. */
451 if (disk->read_hook)
452 while (size)
454 (disk->read_hook) (sector, real_offset,
455 ((size > GRUB_DISK_SECTOR_SIZE)
456 ? GRUB_DISK_SECTOR_SIZE
457 : size));
458 sector++;
459 size -= GRUB_DISK_SECTOR_SIZE - real_offset;
460 real_offset = 0;
463 /* This must be the end. */
464 goto finish;
467 /* Copy it and store it in the disk cache. */
468 grub_memcpy (buf, tmp_buf + pos + real_offset, len);
469 grub_disk_cache_store (disk->dev->id, disk->id,
470 start_sector, tmp_buf);
473 /* Call the read hook, if any. */
474 if (disk->read_hook)
476 grub_disk_addr_t s = sector;
477 grub_size_t l = len;
479 while (l)
481 (disk->read_hook) (s, real_offset,
482 ((l > GRUB_DISK_SECTOR_SIZE)
483 ? GRUB_DISK_SECTOR_SIZE
484 : l));
486 if (l < GRUB_DISK_SECTOR_SIZE - real_offset)
487 break;
489 s++;
490 l -= GRUB_DISK_SECTOR_SIZE - real_offset;
491 real_offset = 0;
495 sector = start_sector + GRUB_DISK_CACHE_SIZE;
496 buf += len;
497 size -= len;
498 real_offset = 0;
501 finish:
503 grub_free (tmp_buf);
505 return grub_errno;
508 grub_err_t
509 grub_disk_write (grub_disk_t disk, grub_disk_addr_t sector,
510 grub_off_t offset, grub_size_t size, const char *buf)
512 unsigned real_offset;
514 grub_dprintf ("disk", "Writing `%s'...\n", disk->name);
516 if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
517 return -1;
519 real_offset = offset;
521 while (size)
523 if (real_offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
525 char tmp_buf[GRUB_DISK_SECTOR_SIZE];
526 grub_size_t len;
528 if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
529 != GRUB_ERR_NONE)
530 goto finish;
532 len = GRUB_DISK_SECTOR_SIZE - real_offset;
533 if (len > size)
534 len = size;
536 grub_memcpy (tmp_buf + real_offset, buf, len);
538 grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
540 if ((disk->dev->write) (disk, sector, 1, tmp_buf) != GRUB_ERR_NONE)
541 goto finish;
543 sector++;
544 buf += len;
545 size -= len;
546 real_offset = 0;
548 else
550 grub_size_t len;
551 grub_size_t n;
553 len = size & ~(GRUB_DISK_SECTOR_SIZE - 1);
554 n = size >> GRUB_DISK_SECTOR_BITS;
556 if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE)
557 goto finish;
559 while (n--)
560 grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
562 buf += len;
563 size -= len;
567 finish:
569 return grub_errno;
572 grub_uint64_t
573 grub_disk_get_size (grub_disk_t disk)
575 if (disk->partition)
576 return grub_partition_get_len (disk->partition);
577 else
578 return disk->total_sectors;