amiga.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/partitions/amiga.c
  4. *
  5. * Code extracted from drivers/block/genhd.c
  6. *
  7. * Copyright (C) 1991-1998 Linus Torvalds
  8. * Re-organised Feb 1998 Russell King
  9. */
  10. #define pr_fmt(fmt) fmt
  11. #include <linux/types.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/overflow.h>
  14. #include <linux/affs_hardblocks.h>
  15. #include "check.h"
  16. /* magic offsets in partition DosEnvVec */
  17. #define NR_HD 3
  18. #define NR_SECT 5
  19. #define LO_CYL 9
  20. #define HI_CYL 10
  21. static __inline__ u32
  22. checksum_block(__be32 *m, int size)
  23. {
  24. u32 sum = 0;
  25. while (size--)
  26. sum += be32_to_cpu(*m++);
  27. return sum;
  28. }
  29. int amiga_partition(struct parsed_partitions *state)
  30. {
  31. Sector sect;
  32. unsigned char *data;
  33. struct RigidDiskBlock *rdb;
  34. struct PartitionBlock *pb;
  35. u64 start_sect, nr_sects;
  36. sector_t blk, end_sect;
  37. u32 cylblk; /* rdb_CylBlocks = nr_heads*sect_per_track */
  38. u32 nr_hd, nr_sect, lo_cyl, hi_cyl;
  39. int part, res = 0;
  40. unsigned int blksize = 1; /* Multiplier for disk block size */
  41. int slot = 1;
  42. for (blk = 0; ; blk++, put_dev_sector(sect)) {
  43. if (blk == RDB_ALLOCATION_LIMIT)
  44. goto rdb_done;
  45. data = read_part_sector(state, blk, &sect);
  46. if (!data) {
  47. pr_err("Dev %s: unable to read RDB block %llu\n",
  48. state->disk->disk_name, blk);
  49. res = -1;
  50. goto rdb_done;
  51. }
  52. if (*(__be32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
  53. continue;
  54. rdb = (struct RigidDiskBlock *)data;
  55. if (checksum_block((__be32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
  56. break;
  57. /* Try again with 0xdc..0xdf zeroed, Windows might have
  58. * trashed it.
  59. */
  60. *(__be32 *)(data+0xdc) = 0;
  61. if (checksum_block((__be32 *)data,
  62. be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
  63. pr_err("Trashed word at 0xd0 in block %llu ignored in checksum calculation\n",
  64. blk);
  65. break;
  66. }
  67. pr_err("Dev %s: RDB in block %llu has bad checksum\n",
  68. state->disk->disk_name, blk);
  69. }
  70. /* blksize is blocks per 512 byte standard block */
  71. blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
  72. {
  73. char tmp[7 + 10 + 1 + 1];
  74. /* Be more informative */
  75. snprintf(tmp, sizeof(tmp), " RDSK (%d)", blksize * 512);
  76. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  77. }
  78. blk = be32_to_cpu(rdb->rdb_PartitionList);
  79. put_dev_sector(sect);
  80. for (part = 1; (s32) blk>0 && part<=16; part++, put_dev_sector(sect)) {
  81. /* Read in terms partition table understands */
  82. if (check_mul_overflow(blk, (sector_t) blksize, &blk)) {
  83. pr_err("Dev %s: overflow calculating partition block %llu! Skipping partitions %u and beyond\n",
  84. state->disk->disk_name, blk, part);
  85. break;
  86. }
  87. data = read_part_sector(state, blk, &sect);
  88. if (!data) {
  89. pr_err("Dev %s: unable to read partition block %llu\n",
  90. state->disk->disk_name, blk);
  91. res = -1;
  92. goto rdb_done;
  93. }
  94. pb = (struct PartitionBlock *)data;
  95. blk = be32_to_cpu(pb->pb_Next);
  96. if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
  97. continue;
  98. if (checksum_block((__be32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
  99. continue;
  100. /* RDB gives us more than enough rope to hang ourselves with,
  101. * many times over (2^128 bytes if all fields max out).
  102. * Some careful checks are in order, so check for potential
  103. * overflows.
  104. * We are multiplying four 32 bit numbers to one sector_t!
  105. */
  106. nr_hd = be32_to_cpu(pb->pb_Environment[NR_HD]);
  107. nr_sect = be32_to_cpu(pb->pb_Environment[NR_SECT]);
  108. /* CylBlocks is total number of blocks per cylinder */
  109. if (check_mul_overflow(nr_hd, nr_sect, &cylblk)) {
  110. pr_err("Dev %s: heads*sects %u overflows u32, skipping partition!\n",
  111. state->disk->disk_name, cylblk);
  112. continue;
  113. }
  114. /* check for consistency with RDB defined CylBlocks */
  115. if (cylblk > be32_to_cpu(rdb->rdb_CylBlocks)) {
  116. pr_warn("Dev %s: cylblk %u > rdb_CylBlocks %u!\n",
  117. state->disk->disk_name, cylblk,
  118. be32_to_cpu(rdb->rdb_CylBlocks));
  119. }
  120. /* RDB allows for variable logical block size -
  121. * normalize to 512 byte blocks and check result.
  122. */
  123. if (check_mul_overflow(cylblk, blksize, &cylblk)) {
  124. pr_err("Dev %s: partition %u bytes per cyl. overflows u32, skipping partition!\n",
  125. state->disk->disk_name, part);
  126. continue;
  127. }
  128. /* Calculate partition start and end. Limit of 32 bit on cylblk
  129. * guarantees no overflow occurs if LBD support is enabled.
  130. */
  131. lo_cyl = be32_to_cpu(pb->pb_Environment[LO_CYL]);
  132. start_sect = ((u64) lo_cyl * cylblk);
  133. hi_cyl = be32_to_cpu(pb->pb_Environment[HI_CYL]);
  134. nr_sects = (((u64) hi_cyl - lo_cyl + 1) * cylblk);
  135. if (!nr_sects)
  136. continue;
  137. /* Warn user if partition end overflows u32 (AmigaDOS limit) */
  138. if ((start_sect + nr_sects) > UINT_MAX) {
  139. pr_warn("Dev %s: partition %u (%llu-%llu) needs 64 bit device support!\n",
  140. state->disk->disk_name, part,
  141. start_sect, start_sect + nr_sects);
  142. }
  143. if (check_add_overflow(start_sect, nr_sects, &end_sect)) {
  144. pr_err("Dev %s: partition %u (%llu-%llu) needs LBD device support, skipping partition!\n",
  145. state->disk->disk_name, part,
  146. start_sect, end_sect);
  147. continue;
  148. }
  149. /* Tell Kernel about it */
  150. put_partition(state,slot++,start_sect,nr_sects);
  151. {
  152. /* Be even more informative to aid mounting */
  153. char dostype[4];
  154. char tmp[42];
  155. __be32 *dt = (__be32 *)dostype;
  156. *dt = pb->pb_Environment[16];
  157. if (dostype[3] < ' ')
  158. snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
  159. dostype[0], dostype[1],
  160. dostype[2], dostype[3] + '@' );
  161. else
  162. snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
  163. dostype[0], dostype[1],
  164. dostype[2], dostype[3]);
  165. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  166. snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
  167. be32_to_cpu(pb->pb_Environment[6]),
  168. be32_to_cpu(pb->pb_Environment[4]));
  169. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  170. }
  171. res = 1;
  172. }
  173. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  174. rdb_done:
  175. return res;
  176. }