part_mac.c 6.1 KB

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