part_mac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ((CONFIG_COMMANDS & CFG_CMD_IDE) || \
  35. (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
  36. (CONFIG_COMMANDS & CFG_CMD_USB) || \
  37. defined(CONFIG_MMC) || \
  38. defined(CONFIG_SYSTEMACE) ) && defined(CONFIG_MAC_PARTITION)
  39. /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  40. #ifndef __ldiv_t_defined
  41. typedef struct {
  42. long int quot; /* Quotient */
  43. long int rem; /* Remainder */
  44. } ldiv_t;
  45. extern ldiv_t ldiv (long int __numer, long int __denom);
  46. # define __ldiv_t_defined 1
  47. #endif
  48. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
  49. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
  50. /*
  51. * Test for a valid MAC partition
  52. */
  53. int test_part_mac (block_dev_desc_t *dev_desc)
  54. {
  55. mac_driver_desc_t ddesc;
  56. mac_partition_t mpart;
  57. ulong i, n;
  58. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  59. /* error reading Driver Desriptor Block, or no valid Signature */
  60. return (-1);
  61. }
  62. n = 1; /* assuming at least one partition */
  63. for (i=1; i<=n; ++i) {
  64. if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)&mpart) != 1) ||
  65. (mpart.signature != MAC_PARTITION_MAGIC) ) {
  66. return (-1);
  67. }
  68. /* update partition count */
  69. n = mpart.map_count;
  70. }
  71. return (0);
  72. }
  73. void print_part_mac (block_dev_desc_t *dev_desc)
  74. {
  75. ulong i, n;
  76. mac_driver_desc_t ddesc;
  77. mac_partition_t mpart;
  78. ldiv_t mb, gb;
  79. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  80. /* error reading Driver Desriptor Block, or no valid Signature */
  81. return;
  82. }
  83. n = ddesc.blk_count;
  84. mb = ldiv(n, ((1024 * 1024) / ddesc.blk_size)); /* MB */
  85. /* round to 1 digit */
  86. mb.rem *= 10 * ddesc.blk_size;
  87. mb.rem += 512 * 1024;
  88. mb.rem /= 1024 * 1024;
  89. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  90. gb.rem += 512;
  91. gb.rem /= 1024;
  92. printf ("Block Size=%d, Number of Blocks=%d, "
  93. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  94. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  95. " #: type name"
  96. " length base (size)\n",
  97. ddesc.blk_size,
  98. ddesc.blk_count,
  99. mb.quot, mb.rem, gb.quot, gb.rem,
  100. ddesc.dev_type, ddesc.dev_id
  101. );
  102. n = 1; /* assuming at least one partition */
  103. for (i=1; i<=n; ++i) {
  104. ulong bytes;
  105. char c;
  106. printf ("%4ld: ", i);
  107. if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)&mpart) != 1) {
  108. printf ("** Can't read Partition Map on %d:%ld **\n",
  109. dev_desc->dev, i);
  110. return;
  111. }
  112. if (mpart.signature != MAC_PARTITION_MAGIC) {
  113. printf ("** Bad Signature on %d:%ld - "
  114. "expected 0x%04x, got 0x%04x\n",
  115. dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart.signature);
  116. return;
  117. }
  118. /* update partition count */
  119. n = mpart.map_count;
  120. c = 'k';
  121. bytes = mpart.block_count;
  122. bytes /= (1024 / ddesc.blk_size); /* kB; assumes blk_size == 512 */
  123. if (bytes >= 1024) {
  124. bytes >>= 10;
  125. c = 'M';
  126. }
  127. if (bytes >= 1024) {
  128. bytes >>= 10;
  129. c = 'G';
  130. }
  131. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  132. mpart.type,
  133. mpart.name,
  134. mpart.block_count,
  135. mpart.start_block,
  136. bytes, c
  137. );
  138. }
  139. return;
  140. }
  141. /*
  142. * Read Device Descriptor Block
  143. */
  144. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
  145. {
  146. if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
  147. printf ("** Can't read Driver Desriptor Block **\n");
  148. return (-1);
  149. }
  150. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  151. #if 0
  152. printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
  153. MAC_DRIVER_MAGIC, ddb_p->signature);
  154. #endif
  155. return (-1);
  156. }
  157. return (0);
  158. }
  159. /*
  160. * Read Partition Descriptor Block
  161. */
  162. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
  163. {
  164. int n = 1;
  165. for (;;) {
  166. /*
  167. * We must always read the descritpor block for
  168. * partition 1 first since this is the only way to
  169. * know how many partitions we have.
  170. */
  171. if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
  172. printf ("** Can't read Partition Map on %d:%d **\n",
  173. dev_desc->dev, n);
  174. return (-1);
  175. }
  176. if (pdb_p->signature != MAC_PARTITION_MAGIC) {
  177. printf ("** Bad Signature on %d:%d: "
  178. "expected 0x%04x, got 0x%04x\n",
  179. dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
  180. return (-1);
  181. }
  182. if (n == part)
  183. return (0);
  184. if ((part < 1) || (part > pdb_p->map_count)) {
  185. printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
  186. dev_desc->dev, part,
  187. dev_desc->dev,
  188. dev_desc->dev, pdb_p->map_count);
  189. return (-1);
  190. }
  191. /* update partition count */
  192. n = part;
  193. }
  194. /* NOTREACHED */
  195. }
  196. int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
  197. {
  198. mac_driver_desc_t ddesc;
  199. mac_partition_t mpart;
  200. if (part_mac_read_ddb (dev_desc, &ddesc)) {
  201. return (-1);
  202. }
  203. info->blksz = ddesc.blk_size;
  204. if (part_mac_read_pdb (dev_desc, part, &mpart)) {
  205. return (-1);
  206. }
  207. info->start = mpart.start_block;
  208. info->size = mpart.block_count;
  209. memcpy (info->type, mpart.type, sizeof(info->type));
  210. memcpy (info->name, mpart.name, sizeof(info->name));
  211. return (0);
  212. }
  213. #endif /* (CONFIG_COMMANDS & CFG_CMD_IDE) && CONFIG_MAC_PARTITION */