part_mac.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Support for harddisk partitions.
  25. *
  26. * To be compatible with LinuxPPC and Apple we use the standard Apple
  27. * SCSI disk partitioning scheme. For more information see:
  28. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  29. */
  30. #include <common.h>
  31. #include <command.h>
  32. #include <ide.h>
  33. #include "part_mac.h"
  34. #if defined(CONFIG_CMD_IDE) || \
  35. defined(CONFIG_CMD_MG_DISK) || \
  36. defined(CONFIG_CMD_SCSI) || \
  37. defined(CONFIG_CMD_SATA) || \
  38. defined(CONFIG_CMD_USB) || \
  39. defined(CONFIG_MMC) || \
  40. defined(CONFIG_SYSTEMACE)
  41. /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  42. #ifndef __ldiv_t_defined
  43. typedef struct {
  44. long int quot; /* Quotient */
  45. long int rem; /* Remainder */
  46. } ldiv_t;
  47. extern ldiv_t ldiv (long int __numer, long int __denom);
  48. # define __ldiv_t_defined 1
  49. #endif
  50. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
  51. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
  52. /*
  53. * Test for a valid MAC partition
  54. */
  55. int test_part_mac (block_dev_desc_t *dev_desc)
  56. {
  57. mac_driver_desc_t ddesc;
  58. mac_partition_t mpart;
  59. ulong i, n;
  60. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  61. /* error reading Driver Desriptor Block, or no valid Signature */
  62. return (-1);
  63. }
  64. n = 1; /* assuming at least one partition */
  65. for (i=1; i<=n; ++i) {
  66. if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)&mpart) != 1) ||
  67. (mpart.signature != MAC_PARTITION_MAGIC) ) {
  68. return (-1);
  69. }
  70. /* update partition count */
  71. n = mpart.map_count;
  72. }
  73. return (0);
  74. }
  75. void print_part_mac (block_dev_desc_t *dev_desc)
  76. {
  77. ulong i, n;
  78. mac_driver_desc_t ddesc;
  79. mac_partition_t mpart;
  80. ldiv_t mb, gb;
  81. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  82. /* error reading Driver Desriptor Block, or no valid Signature */
  83. return;
  84. }
  85. n = ddesc.blk_count;
  86. mb = ldiv(n, ((1024 * 1024) / ddesc.blk_size)); /* MB */
  87. /* round to 1 digit */
  88. mb.rem *= 10 * ddesc.blk_size;
  89. mb.rem += 512 * 1024;
  90. mb.rem /= 1024 * 1024;
  91. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  92. gb.rem += 512;
  93. gb.rem /= 1024;
  94. printf ("Block Size=%d, Number of Blocks=%d, "
  95. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  96. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  97. " #: type name"
  98. " length base (size)\n",
  99. ddesc.blk_size,
  100. ddesc.blk_count,
  101. mb.quot, mb.rem, gb.quot, gb.rem,
  102. ddesc.dev_type, ddesc.dev_id
  103. );
  104. n = 1; /* assuming at least one partition */
  105. for (i=1; i<=n; ++i) {
  106. ulong bytes;
  107. char c;
  108. printf ("%4ld: ", i);
  109. if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)&mpart) != 1) {
  110. printf ("** Can't read Partition Map on %d:%ld **\n",
  111. dev_desc->dev, i);
  112. return;
  113. }
  114. if (mpart.signature != MAC_PARTITION_MAGIC) {
  115. printf ("** Bad Signature on %d:%ld - "
  116. "expected 0x%04x, got 0x%04x\n",
  117. dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart.signature);
  118. return;
  119. }
  120. /* update partition count */
  121. n = mpart.map_count;
  122. c = 'k';
  123. bytes = mpart.block_count;
  124. bytes /= (1024 / ddesc.blk_size); /* kB; assumes blk_size == 512 */
  125. if (bytes >= 1024) {
  126. bytes >>= 10;
  127. c = 'M';
  128. }
  129. if (bytes >= 1024) {
  130. bytes >>= 10;
  131. c = 'G';
  132. }
  133. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  134. mpart.type,
  135. mpart.name,
  136. mpart.block_count,
  137. mpart.start_block,
  138. bytes, c
  139. );
  140. }
  141. return;
  142. }
  143. /*
  144. * Read Device Descriptor Block
  145. */
  146. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
  147. {
  148. if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
  149. printf ("** Can't read Driver Desriptor Block **\n");
  150. return (-1);
  151. }
  152. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  153. #if 0
  154. printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
  155. MAC_DRIVER_MAGIC, ddb_p->signature);
  156. #endif
  157. return (-1);
  158. }
  159. return (0);
  160. }
  161. /*
  162. * Read Partition Descriptor Block
  163. */
  164. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
  165. {
  166. int n = 1;
  167. for (;;) {
  168. /*
  169. * We must always read the descritpor block for
  170. * partition 1 first since this is the only way to
  171. * know how many partitions we have.
  172. */
  173. if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
  174. printf ("** Can't read Partition Map on %d:%d **\n",
  175. dev_desc->dev, n);
  176. return (-1);
  177. }
  178. if (pdb_p->signature != MAC_PARTITION_MAGIC) {
  179. printf ("** Bad Signature on %d:%d: "
  180. "expected 0x%04x, got 0x%04x\n",
  181. dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
  182. return (-1);
  183. }
  184. if (n == part)
  185. return (0);
  186. if ((part < 1) || (part > pdb_p->map_count)) {
  187. printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
  188. dev_desc->dev, part,
  189. dev_desc->dev,
  190. dev_desc->dev, pdb_p->map_count);
  191. return (-1);
  192. }
  193. /* update partition count */
  194. n = part;
  195. }
  196. /* NOTREACHED */
  197. }
  198. int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
  199. {
  200. mac_driver_desc_t ddesc;
  201. mac_partition_t mpart;
  202. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  203. return (-1);
  204. }
  205. info->blksz = ddesc.blk_size;
  206. if (part_mac_read_pdb (dev_desc, part, &mpart)) {
  207. return (-1);
  208. }
  209. info->start = mpart.start_block;
  210. info->size = mpart.block_count;
  211. memcpy (info->type, mpart.type, sizeof(info->type));
  212. memcpy (info->name, mpart.name, sizeof(info->name));
  213. return (0);
  214. }
  215. #endif