part_mac.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Support for harddisk partitions.
  8. *
  9. * To be compatible with LinuxPPC and Apple we use the standard Apple
  10. * SCSI disk partitioning scheme. For more information see:
  11. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <memalign.h>
  16. #include <ide.h>
  17. #include "part_mac.h"
  18. #include <part.h>
  19. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  20. /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  21. #ifndef __ldiv_t_defined
  22. typedef struct {
  23. long int quot; /* Quotient */
  24. long int rem; /* Remainder */
  25. } ldiv_t;
  26. extern ldiv_t ldiv (long int __numer, long int __denom);
  27. # define __ldiv_t_defined 1
  28. #endif
  29. static int part_mac_read_ddb(struct blk_desc *dev_desc,
  30. mac_driver_desc_t *ddb_p);
  31. static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
  32. mac_partition_t *pdb_p);
  33. /*
  34. * Test for a valid MAC partition
  35. */
  36. static int part_test_mac(struct blk_desc *dev_desc)
  37. {
  38. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  39. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  40. ulong i, n;
  41. if (part_mac_read_ddb (dev_desc, ddesc)) {
  42. /*
  43. * error reading Driver Descriptor Block,
  44. * or no valid Signature
  45. */
  46. return (-1);
  47. }
  48. n = 1; /* assuming at least one partition */
  49. for (i=1; i<=n; ++i) {
  50. if ((blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) ||
  51. (mpart->signature != MAC_PARTITION_MAGIC) ) {
  52. return (-1);
  53. }
  54. /* update partition count */
  55. n = mpart->map_count;
  56. }
  57. return (0);
  58. }
  59. static void part_print_mac(struct blk_desc *dev_desc)
  60. {
  61. ulong i, n;
  62. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  63. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  64. ldiv_t mb, gb;
  65. if (part_mac_read_ddb (dev_desc, ddesc)) {
  66. /*
  67. * error reading Driver Descriptor Block,
  68. * or no valid Signature
  69. */
  70. return;
  71. }
  72. n = ddesc->blk_count;
  73. mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
  74. /* round to 1 digit */
  75. mb.rem *= 10 * ddesc->blk_size;
  76. mb.rem += 512 * 1024;
  77. mb.rem /= 1024 * 1024;
  78. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  79. gb.rem += 512;
  80. gb.rem /= 1024;
  81. printf ("Block Size=%d, Number of Blocks=%d, "
  82. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  83. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  84. " #: type name"
  85. " length base (size)\n",
  86. ddesc->blk_size,
  87. ddesc->blk_count,
  88. mb.quot, mb.rem, gb.quot, gb.rem,
  89. ddesc->dev_type, ddesc->dev_id
  90. );
  91. n = 1; /* assuming at least one partition */
  92. for (i=1; i<=n; ++i) {
  93. ulong bytes;
  94. char c;
  95. printf ("%4ld: ", i);
  96. if (blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) {
  97. printf ("** Can't read Partition Map on %d:%ld **\n",
  98. dev_desc->devnum, i);
  99. return;
  100. }
  101. if (mpart->signature != MAC_PARTITION_MAGIC) {
  102. printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
  103. dev_desc->devnum, i, MAC_PARTITION_MAGIC,
  104. mpart->signature);
  105. return;
  106. }
  107. /* update partition count */
  108. n = mpart->map_count;
  109. c = 'k';
  110. bytes = mpart->block_count;
  111. bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
  112. if (bytes >= 1024) {
  113. bytes >>= 10;
  114. c = 'M';
  115. }
  116. if (bytes >= 1024) {
  117. bytes >>= 10;
  118. c = 'G';
  119. }
  120. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  121. mpart->type,
  122. mpart->name,
  123. mpart->block_count,
  124. mpart->start_block,
  125. bytes, c
  126. );
  127. }
  128. return;
  129. }
  130. /*
  131. * Read Device Descriptor Block
  132. */
  133. static int part_mac_read_ddb(struct blk_desc *dev_desc,
  134. mac_driver_desc_t *ddb_p)
  135. {
  136. if (blk_dread(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
  137. debug("** Can't read Driver Descriptor Block **\n");
  138. return (-1);
  139. }
  140. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  141. return (-1);
  142. }
  143. return (0);
  144. }
  145. /*
  146. * Read Partition Descriptor Block
  147. */
  148. static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
  149. mac_partition_t *pdb_p)
  150. {
  151. int n = 1;
  152. for (;;) {
  153. /*
  154. * We must always read the descritpor block for
  155. * partition 1 first since this is the only way to
  156. * know how many partitions we have.
  157. */
  158. if (blk_dread(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
  159. printf ("** Can't read Partition Map on %d:%d **\n",
  160. dev_desc->devnum, n);
  161. return (-1);
  162. }
  163. if (pdb_p->signature != MAC_PARTITION_MAGIC) {
  164. printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n",
  165. dev_desc->devnum, n, MAC_PARTITION_MAGIC,
  166. pdb_p->signature);
  167. return (-1);
  168. }
  169. if (n == part)
  170. return (0);
  171. if ((part < 1) || (part > pdb_p->map_count)) {
  172. printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
  173. dev_desc->devnum, part,
  174. dev_desc->devnum,
  175. dev_desc->devnum, pdb_p->map_count);
  176. return (-1);
  177. }
  178. /* update partition count */
  179. n = part;
  180. }
  181. /* NOTREACHED */
  182. }
  183. static int part_get_info_mac(struct blk_desc *dev_desc, int part,
  184. struct disk_partition *info)
  185. {
  186. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  187. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  188. if (part_mac_read_ddb (dev_desc, ddesc)) {
  189. return (-1);
  190. }
  191. info->blksz = ddesc->blk_size;
  192. if (part_mac_read_pdb (dev_desc, part, mpart)) {
  193. return (-1);
  194. }
  195. info->start = mpart->start_block;
  196. info->size = mpart->block_count;
  197. memcpy (info->type, mpart->type, sizeof(info->type));
  198. memcpy (info->name, mpart->name, sizeof(info->name));
  199. return (0);
  200. }
  201. U_BOOT_PART_TYPE(mac) = {
  202. .name = "MAC",
  203. .part_type = PART_TYPE_MAC,
  204. .max_entries = MAC_ENTRY_NUMBERS,
  205. .get_info = part_get_info_mac,
  206. .print = part_print_mac,
  207. .test = part_test_mac,
  208. };
  209. #endif