amiga.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/affs_hardblocks.h>
  13. #include "check.h"
  14. #include "amiga.h"
  15. static __inline__ u32
  16. checksum_block(__be32 *m, int size)
  17. {
  18. u32 sum = 0;
  19. while (size--)
  20. sum += be32_to_cpu(*m++);
  21. return sum;
  22. }
  23. int amiga_partition(struct parsed_partitions *state)
  24. {
  25. Sector sect;
  26. unsigned char *data;
  27. struct RigidDiskBlock *rdb;
  28. struct PartitionBlock *pb;
  29. int start_sect, nr_sects, blk, part, res = 0;
  30. int blksize = 1; /* Multiplier for disk block size */
  31. int slot = 1;
  32. char b[BDEVNAME_SIZE];
  33. for (blk = 0; ; blk++, put_dev_sector(sect)) {
  34. if (blk == RDB_ALLOCATION_LIMIT)
  35. goto rdb_done;
  36. data = read_part_sector(state, blk, &sect);
  37. if (!data) {
  38. if (warn_no_part)
  39. pr_err("Dev %s: unable to read RDB block %d\n",
  40. bdevname(state->bdev, b), blk);
  41. res = -1;
  42. goto rdb_done;
  43. }
  44. if (*(__be32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
  45. continue;
  46. rdb = (struct RigidDiskBlock *)data;
  47. if (checksum_block((__be32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
  48. break;
  49. /* Try again with 0xdc..0xdf zeroed, Windows might have
  50. * trashed it.
  51. */
  52. *(__be32 *)(data+0xdc) = 0;
  53. if (checksum_block((__be32 *)data,
  54. be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
  55. pr_err("Trashed word at 0xd0 in block %d ignored in checksum calculation\n",
  56. blk);
  57. break;
  58. }
  59. pr_err("Dev %s: RDB in block %d has bad checksum\n",
  60. bdevname(state->bdev, b), blk);
  61. }
  62. /* blksize is blocks per 512 byte standard block */
  63. blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
  64. {
  65. char tmp[7 + 10 + 1 + 1];
  66. /* Be more informative */
  67. snprintf(tmp, sizeof(tmp), " RDSK (%d)", blksize * 512);
  68. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  69. }
  70. blk = be32_to_cpu(rdb->rdb_PartitionList);
  71. put_dev_sector(sect);
  72. for (part = 1; blk>0 && part<=16; part++, put_dev_sector(sect)) {
  73. blk *= blksize; /* Read in terms partition table understands */
  74. data = read_part_sector(state, blk, &sect);
  75. if (!data) {
  76. if (warn_no_part)
  77. pr_err("Dev %s: unable to read partition block %d\n",
  78. bdevname(state->bdev, b), blk);
  79. res = -1;
  80. goto rdb_done;
  81. }
  82. pb = (struct PartitionBlock *)data;
  83. blk = be32_to_cpu(pb->pb_Next);
  84. if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
  85. continue;
  86. if (checksum_block((__be32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
  87. continue;
  88. /* Tell Kernel about it */
  89. nr_sects = (be32_to_cpu(pb->pb_Environment[10]) + 1 -
  90. be32_to_cpu(pb->pb_Environment[9])) *
  91. be32_to_cpu(pb->pb_Environment[3]) *
  92. be32_to_cpu(pb->pb_Environment[5]) *
  93. blksize;
  94. if (!nr_sects)
  95. continue;
  96. start_sect = be32_to_cpu(pb->pb_Environment[9]) *
  97. be32_to_cpu(pb->pb_Environment[3]) *
  98. be32_to_cpu(pb->pb_Environment[5]) *
  99. blksize;
  100. put_partition(state,slot++,start_sect,nr_sects);
  101. {
  102. /* Be even more informative to aid mounting */
  103. char dostype[4];
  104. char tmp[42];
  105. __be32 *dt = (__be32 *)dostype;
  106. *dt = pb->pb_Environment[16];
  107. if (dostype[3] < ' ')
  108. snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
  109. dostype[0], dostype[1],
  110. dostype[2], dostype[3] + '@' );
  111. else
  112. snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
  113. dostype[0], dostype[1],
  114. dostype[2], dostype[3]);
  115. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  116. snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
  117. be32_to_cpu(pb->pb_Environment[6]),
  118. be32_to_cpu(pb->pb_Environment[4]));
  119. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  120. }
  121. res = 1;
  122. }
  123. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  124. rdb_done:
  125. return res;
  126. }