make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / kern / disk.c
blob2529e8075110614b4df5d4ec45399b522da3b13f
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/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;
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) (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 unsigned long 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_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;
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_rtc ();
320 grub_free (disk->partition);
321 grub_free ((void *) disk->name);
322 grub_free (disk);
325 static grub_err_t
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;
332 if (disk->partition)
334 grub_disk_addr_t start;
335 grub_uint64_t len;
337 start = grub_partition_get_start (disk->partition);
338 len = grub_partition_get_len (disk->partition);
340 if (*sector >= len
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");
345 *sector += start;
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. */
357 grub_err_t
358 grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
359 grub_off_t offset, grub_size_t size, char *buf)
361 char *tmp_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, &sector, &offset, size) != GRUB_ERR_NONE)
369 grub_error_push ();
370 grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
371 (unsigned long long) sector, grub_errmsg);
372 grub_error_pop ();
373 return grub_errno;
376 real_offset = offset;
378 /* Allocate a temporary buffer. */
379 tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
380 if (! tmp_buf)
381 return grub_errno;
383 /* Until SIZE is zero... */
384 while (size)
386 char *data;
387 grub_disk_addr_t start_sector;
388 grub_size_t len;
389 grub_size_t pos;
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);
396 if (len > size)
397 len = size;
399 /* Fetch the cache. */
400 data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector);
401 if (data)
403 /* Just copy it! */
404 grub_memcpy (buf, data + pos + real_offset, len);
405 grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector);
407 else
409 /* Otherwise read data from the disk actually. */
410 if ((disk->dev->read) (disk, start_sector,
411 GRUB_DISK_CACHE_SIZE, tmp_buf)
412 != GRUB_ERR_NONE)
414 /* Uggh... Failed. Instead, just read necessary data. */
415 unsigned num;
416 char *p;
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);
424 if (!p)
425 goto finish;
427 tmp_buf = p;
429 if ((disk->dev->read) (disk, sector, num, tmp_buf))
431 grub_error_push ();
432 grub_dprintf ("disk", "%s read failed\n", disk->name);
433 grub_error_pop ();
434 goto finish;
437 grub_memcpy (buf, tmp_buf + real_offset, size);
439 /* Call the read hook, if any. */
440 if (disk->read_hook)
441 while (size)
443 (disk->read_hook) (sector, real_offset,
444 ((size > GRUB_DISK_SECTOR_SIZE)
445 ? GRUB_DISK_SECTOR_SIZE
446 : size));
447 sector++;
448 size -= GRUB_DISK_SECTOR_SIZE - real_offset;
449 real_offset = 0;
452 /* This must be the end. */
453 goto finish;
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. */
463 if (disk->read_hook)
465 grub_disk_addr_t s = sector;
466 grub_size_t l = len;
468 while (l)
470 (disk->read_hook) (s, real_offset,
471 ((l > GRUB_DISK_SECTOR_SIZE)
472 ? GRUB_DISK_SECTOR_SIZE
473 : l));
475 if (l < GRUB_DISK_SECTOR_SIZE - real_offset)
476 break;
478 s++;
479 l -= GRUB_DISK_SECTOR_SIZE - real_offset;
480 real_offset = 0;
484 sector = start_sector + GRUB_DISK_CACHE_SIZE;
485 buf += len;
486 size -= len;
487 real_offset = 0;
490 finish:
492 grub_free (tmp_buf);
494 return grub_errno;
497 grub_err_t
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, &sector, &offset, size) != GRUB_ERR_NONE)
506 return -1;
508 real_offset = offset;
510 while (size)
512 if (real_offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
514 char tmp_buf[GRUB_DISK_SECTOR_SIZE];
515 grub_size_t len;
517 if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
518 != GRUB_ERR_NONE)
519 goto finish;
521 len = GRUB_DISK_SECTOR_SIZE - real_offset;
522 if (len > size)
523 len = size;
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)
530 goto finish;
532 sector++;
533 buf += len;
534 size -= len;
535 real_offset = 0;
537 else
539 grub_size_t len;
540 grub_size_t n;
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)
546 goto finish;
548 while (n--)
549 grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
551 buf += len;
552 size -= len;
556 finish:
558 return grub_errno;
561 grub_uint64_t
562 grub_disk_get_size (grub_disk_t disk)
564 if (disk->partition)
565 return grub_partition_get_len (disk->partition);
566 else
567 return disk->total_sectors;