part_mac.c 6.1 KB

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