nand.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* nand.c - NAND flash disk access. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009 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/misc.h>
  20. #include <grub/disk.h>
  21. #include <grub/mm.h>
  22. #include <grub/dl.h>
  23. #include <grub/ieee1275/ieee1275.h>
  24. #include <grub/i18n.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. struct grub_nand_data
  27. {
  28. grub_ieee1275_ihandle_t handle;
  29. grub_uint32_t block_size;
  30. };
  31. static int
  32. grub_nand_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  33. grub_disk_pull_t pull)
  34. {
  35. static int have_nand = -1;
  36. if (pull != GRUB_DISK_PULL_NONE)
  37. return 0;
  38. if (have_nand == -1)
  39. {
  40. struct grub_ieee1275_devalias alias;
  41. have_nand = 0;
  42. FOR_IEEE1275_DEVALIASES(alias)
  43. if (grub_strcmp (alias.name, "nand") == 0)
  44. {
  45. have_nand = 1;
  46. break;
  47. }
  48. grub_ieee1275_devalias_free (&alias);
  49. }
  50. if (have_nand)
  51. return hook ("nand", hook_data);
  52. return 0;
  53. }
  54. static grub_err_t
  55. grub_nand_read (grub_disk_t disk, grub_disk_addr_t sector,
  56. grub_size_t size, char *buf);
  57. static grub_err_t
  58. grub_nand_open (const char *name, grub_disk_t disk)
  59. {
  60. grub_ieee1275_ihandle_t dev_ihandle = 0;
  61. struct grub_nand_data *data = 0;
  62. const char *devname;
  63. struct size_args
  64. {
  65. struct grub_ieee1275_common_hdr common;
  66. grub_ieee1275_cell_t method;
  67. grub_ieee1275_cell_t ihandle;
  68. grub_ieee1275_cell_t result;
  69. grub_ieee1275_cell_t size1;
  70. grub_ieee1275_cell_t size2;
  71. } args;
  72. if (grub_memcmp (name, "nand/", sizeof ("nand/") - 1) == 0)
  73. devname = name + sizeof ("nand/") - 1;
  74. else if (grub_strcmp (name, "nand") == 0)
  75. devname = name;
  76. else
  77. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a NAND device");
  78. data = grub_malloc (sizeof (*data));
  79. if (! data)
  80. goto fail;
  81. grub_ieee1275_open (devname, &dev_ihandle);
  82. if (! dev_ihandle)
  83. {
  84. grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
  85. goto fail;
  86. }
  87. data->handle = dev_ihandle;
  88. INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
  89. args.method = (grub_ieee1275_cell_t) "block-size";
  90. args.ihandle = dev_ihandle;
  91. args.result = 1;
  92. if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
  93. {
  94. grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't get block size");
  95. goto fail;
  96. }
  97. data->block_size = (args.size1 >> GRUB_DISK_SECTOR_BITS);
  98. if (!data->block_size)
  99. {
  100. grub_error (GRUB_ERR_UNKNOWN_DEVICE, "invalid block size");
  101. goto fail;
  102. }
  103. INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
  104. args.method = (grub_ieee1275_cell_t) "size";
  105. args.ihandle = dev_ihandle;
  106. args.result = 1;
  107. if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
  108. {
  109. grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't get disk size");
  110. goto fail;
  111. }
  112. disk->total_sectors = args.size1;
  113. disk->total_sectors <<= 32;
  114. disk->total_sectors += args.size2;
  115. disk->total_sectors >>= GRUB_DISK_SECTOR_BITS;
  116. disk->id = dev_ihandle;
  117. disk->data = data;
  118. return 0;
  119. fail:
  120. if (dev_ihandle)
  121. grub_ieee1275_close (dev_ihandle);
  122. grub_free (data);
  123. return grub_errno;
  124. }
  125. static void
  126. grub_nand_close (grub_disk_t disk)
  127. {
  128. grub_ieee1275_close (((struct grub_nand_data *) disk->data)->handle);
  129. grub_free (disk->data);
  130. }
  131. static grub_err_t
  132. grub_nand_read (grub_disk_t disk, grub_disk_addr_t sector,
  133. grub_size_t size, char *buf)
  134. {
  135. struct grub_nand_data *data = disk->data;
  136. grub_size_t bsize, ofs;
  137. struct read_args
  138. {
  139. struct grub_ieee1275_common_hdr common;
  140. grub_ieee1275_cell_t method;
  141. grub_ieee1275_cell_t ihandle;
  142. grub_ieee1275_cell_t ofs;
  143. grub_ieee1275_cell_t page;
  144. grub_ieee1275_cell_t len;
  145. grub_ieee1275_cell_t buf;
  146. grub_ieee1275_cell_t result;
  147. } args;
  148. INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
  149. args.method = (grub_ieee1275_cell_t) "pio-read";
  150. args.ihandle = data->handle;
  151. args.buf = (grub_ieee1275_cell_t) buf;
  152. args.page = (grub_ieee1275_cell_t) ((grub_size_t) sector / data->block_size);
  153. ofs = ((grub_size_t) sector % data->block_size) << GRUB_DISK_SECTOR_BITS;
  154. size <<= GRUB_DISK_SECTOR_BITS;
  155. bsize = (data->block_size << GRUB_DISK_SECTOR_BITS);
  156. do
  157. {
  158. grub_size_t len;
  159. len = (ofs + size > bsize) ? (bsize - ofs) : size;
  160. args.len = (grub_ieee1275_cell_t) len;
  161. args.ofs = (grub_ieee1275_cell_t) ofs;
  162. args.result = 1;
  163. if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
  164. return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
  165. "from `%s'"),
  166. (unsigned long long) sector,
  167. disk->name);
  168. ofs = 0;
  169. size -= len;
  170. args.buf += len;
  171. args.page++;
  172. } while (size);
  173. return GRUB_ERR_NONE;
  174. }
  175. static grub_err_t
  176. grub_nand_write (grub_disk_t disk __attribute ((unused)),
  177. grub_disk_addr_t sector __attribute ((unused)),
  178. grub_size_t size __attribute ((unused)),
  179. const char *buf __attribute ((unused)))
  180. {
  181. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  182. "nand write is not supported");
  183. }
  184. static struct grub_disk_dev grub_nand_dev =
  185. {
  186. .name = "nand",
  187. .id = GRUB_DISK_DEVICE_NAND_ID,
  188. .disk_iterate = grub_nand_iterate,
  189. .disk_open = grub_nand_open,
  190. .disk_close = grub_nand_close,
  191. .disk_read = grub_nand_read,
  192. .disk_write = grub_nand_write,
  193. .next = 0
  194. };
  195. GRUB_MOD_INIT(nand)
  196. {
  197. grub_disk_dev_register (&grub_nand_dev);
  198. }
  199. GRUB_MOD_FINI(nand)
  200. {
  201. grub_disk_dev_unregister (&grub_nand_dev);
  202. }