amiga.c 3.6 KB

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