ubootdisk.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* ubootdisk.c - disk subsystem support for U-Boot platforms */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/disk.h>
  20. #include <grub/err.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/partition.h>
  24. #include <grub/term.h>
  25. #include <grub/types.h>
  26. #include <grub/uboot/disk.h>
  27. #include <grub/uboot/uboot.h>
  28. #include <grub/uboot/api_public.h>
  29. static struct ubootdisk_data *hd_devices;
  30. static int hd_num;
  31. static int hd_max;
  32. /*
  33. * grub_ubootdisk_register():
  34. * Called for each disk device enumerated as part of U-Boot initialization
  35. * code.
  36. */
  37. grub_err_t
  38. grub_ubootdisk_register (struct device_info *newdev)
  39. {
  40. struct ubootdisk_data *d;
  41. #define STOR_TYPE(x) ((x) & 0x0ff0)
  42. switch (STOR_TYPE (newdev->type))
  43. {
  44. case DT_STOR_IDE:
  45. case DT_STOR_SATA:
  46. case DT_STOR_SCSI:
  47. case DT_STOR_MMC:
  48. case DT_STOR_USB:
  49. /* hd */
  50. if (hd_num == hd_max)
  51. {
  52. int new_num;
  53. new_num = (hd_max ? hd_max * 2 : 1);
  54. d = grub_realloc(hd_devices,
  55. sizeof (struct ubootdisk_data) * new_num);
  56. if (!d)
  57. return grub_errno;
  58. hd_devices = d;
  59. hd_max = new_num;
  60. }
  61. d = &hd_devices[hd_num];
  62. hd_num++;
  63. break;
  64. default:
  65. return GRUB_ERR_BAD_DEVICE;
  66. break;
  67. }
  68. d->dev = newdev;
  69. d->cookie = newdev->cookie;
  70. d->opencount = 0;
  71. return 0;
  72. }
  73. /*
  74. * uboot_disk_iterate():
  75. * Iterator over enumerated disk devices.
  76. */
  77. static int
  78. uboot_disk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  79. grub_disk_pull_t pull)
  80. {
  81. char buf[16];
  82. int count;
  83. switch (pull)
  84. {
  85. case GRUB_DISK_PULL_NONE:
  86. /* "hd" - built-in mass-storage */
  87. for (count = 0 ; count < hd_num; count++)
  88. {
  89. grub_snprintf (buf, sizeof (buf) - 1, "hd%d", count);
  90. grub_dprintf ("ubootdisk", "iterating %s\n", buf);
  91. if (hook (buf, hook_data))
  92. return 1;
  93. }
  94. break;
  95. default:
  96. return 0;
  97. }
  98. return 0;
  99. }
  100. /* Helper function for uboot_disk_open. */
  101. static struct ubootdisk_data *
  102. get_hd_device (int num)
  103. {
  104. if (num < hd_num)
  105. return &hd_devices[num];
  106. return NULL;
  107. }
  108. /*
  109. * uboot_disk_open():
  110. * Opens a disk device already enumerated.
  111. */
  112. static grub_err_t
  113. uboot_disk_open (const char *name, struct grub_disk *disk)
  114. {
  115. struct ubootdisk_data *d;
  116. struct device_info *devinfo;
  117. int num;
  118. int retval;
  119. grub_dprintf ("ubootdisk", "Opening '%s'\n", name);
  120. num = grub_strtoul (name + 2, 0, 10);
  121. if (grub_errno != GRUB_ERR_NONE)
  122. {
  123. grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid number\n",
  124. name);
  125. goto fail;
  126. }
  127. if (name[1] != 'd')
  128. {
  129. grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid name\n", name);
  130. goto fail;
  131. }
  132. switch (name[0])
  133. {
  134. case 'h':
  135. d = get_hd_device (num);
  136. break;
  137. default:
  138. goto fail;
  139. }
  140. if (!d)
  141. goto fail;
  142. /*
  143. * Subsystems may call open on the same device recursively - but U-Boot
  144. * does not deal with this. So simply keep track of number of calls and
  145. * return success if already open.
  146. */
  147. if (d->opencount > 0)
  148. {
  149. grub_dprintf ("ubootdisk", "(%s) already open\n", disk->name);
  150. d->opencount++;
  151. retval = 0;
  152. }
  153. else
  154. {
  155. retval = grub_uboot_dev_open (d->dev);
  156. if (retval != 0)
  157. goto fail;
  158. d->opencount = 1;
  159. }
  160. grub_dprintf ("ubootdisk", "cookie: 0x%08x\n", (grub_addr_t) d->cookie);
  161. disk->id = (grub_addr_t) d->cookie;
  162. devinfo = d->dev;
  163. d->block_size = devinfo->di_stor.block_size;
  164. if (d->block_size == 0)
  165. return grub_error (GRUB_ERR_IO, "no block size");
  166. disk->log_sector_size = grub_log2ull (d->block_size);
  167. grub_dprintf ("ubootdisk", "(%s) blocksize=%d, log_sector_size=%d\n",
  168. disk->name, d->block_size, disk->log_sector_size);
  169. if (devinfo->di_stor.block_count)
  170. disk->total_sectors = devinfo->di_stor.block_count;
  171. else
  172. disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
  173. disk->data = d;
  174. return GRUB_ERR_NONE;
  175. fail:
  176. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such device");
  177. }
  178. static void
  179. uboot_disk_close (struct grub_disk *disk)
  180. {
  181. struct ubootdisk_data *d;
  182. int retval;
  183. d = disk->data;
  184. /*
  185. * In mirror of open function, keep track of number of calls to close and
  186. * send on to U-Boot only when opencount would decrease to 0.
  187. */
  188. if (d->opencount > 1)
  189. {
  190. grub_dprintf ("ubootdisk", "Closed (%s)\n", disk->name);
  191. d->opencount--;
  192. }
  193. else if (d->opencount == 1)
  194. {
  195. retval = grub_uboot_dev_close (d->dev);
  196. d->opencount--;
  197. grub_dprintf ("ubootdisk", "closed %s (%d)\n", disk->name, retval);
  198. }
  199. else
  200. {
  201. grub_dprintf ("ubootdisk", "device %s not open!\n", disk->name);
  202. }
  203. }
  204. /*
  205. * uboot_disk_read():
  206. * Called from within disk subsystem to read a sequence of blocks into the
  207. * disk cache. Maps directly on top of U-Boot API, only wrap in some error
  208. * handling.
  209. */
  210. static grub_err_t
  211. uboot_disk_read (struct grub_disk *disk,
  212. grub_disk_addr_t offset, grub_size_t numblocks, char *buf)
  213. {
  214. struct ubootdisk_data *d;
  215. grub_size_t real_size;
  216. int retval;
  217. d = disk->data;
  218. retval = grub_uboot_dev_read (d->dev, buf, numblocks, offset, &real_size);
  219. grub_dprintf ("ubootdisk",
  220. "retval=%d, numblocks=%d, real_size=%llu, sector=%llu\n",
  221. retval, numblocks, (grub_uint64_t) real_size,
  222. (grub_uint64_t) offset);
  223. if (retval != 0)
  224. return grub_error (GRUB_ERR_IO, "U-Boot disk read error");
  225. return GRUB_ERR_NONE;
  226. }
  227. static grub_err_t
  228. uboot_disk_write (struct grub_disk *disk,
  229. grub_disk_addr_t offset, grub_size_t numblocks, const char *buf)
  230. {
  231. struct ubootdisk_data *d;
  232. int retval;
  233. d = disk->data;
  234. retval = grub_uboot_dev_write (d->dev, buf, numblocks, offset);
  235. grub_dprintf ("ubootdisk",
  236. "retval=%d, numblocks=%d, sector=%llu\n",
  237. retval, numblocks, (grub_uint64_t) offset);
  238. if (retval != 0)
  239. return grub_error (GRUB_ERR_IO, "U-Boot disk write error");
  240. return GRUB_ERR_NONE;
  241. }
  242. static struct grub_disk_dev grub_ubootdisk_dev = {
  243. .name = "ubootdisk",
  244. .id = GRUB_DISK_DEVICE_UBOOTDISK_ID,
  245. .disk_iterate = uboot_disk_iterate,
  246. .disk_open = uboot_disk_open,
  247. .disk_close = uboot_disk_close,
  248. .disk_read = uboot_disk_read,
  249. .disk_write = uboot_disk_write,
  250. .next = 0
  251. };
  252. void
  253. grub_ubootdisk_init (void)
  254. {
  255. grub_disk_dev_register (&grub_ubootdisk_dev);
  256. }
  257. void
  258. grub_ubootdisk_fini (void)
  259. {
  260. grub_disk_dev_unregister (&grub_ubootdisk_dev);
  261. }