atari.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/partitions/atari.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. #include <linux/ctype.h>
  11. #include "check.h"
  12. #include "atari.h"
  13. /* ++guenther: this should be settable by the user ("make config")?.
  14. */
  15. #define ICD_PARTS
  16. /* check if a partition entry looks valid -- Atari format is assumed if at
  17. least one of the primary entries is ok this way */
  18. #define VALID_PARTITION(pi,hdsiz) \
  19. (((pi)->flg & 1) && \
  20. isalnum((pi)->id[0]) && isalnum((pi)->id[1]) && isalnum((pi)->id[2]) && \
  21. be32_to_cpu((pi)->st) <= (hdsiz) && \
  22. be32_to_cpu((pi)->st) + be32_to_cpu((pi)->siz) <= (hdsiz))
  23. static inline int OK_id(char *s)
  24. {
  25. return memcmp (s, "GEM", 3) == 0 || memcmp (s, "BGM", 3) == 0 ||
  26. memcmp (s, "LNX", 3) == 0 || memcmp (s, "SWP", 3) == 0 ||
  27. memcmp (s, "RAW", 3) == 0 ;
  28. }
  29. int atari_partition(struct parsed_partitions *state)
  30. {
  31. Sector sect;
  32. struct rootsector *rs;
  33. struct partition_info *pi;
  34. u32 extensect;
  35. u32 hd_size;
  36. int slot;
  37. #ifdef ICD_PARTS
  38. int part_fmt = 0; /* 0:unknown, 1:AHDI, 2:ICD/Supra */
  39. #endif
  40. /*
  41. * ATARI partition scheme supports 512 lba only. If this is not
  42. * the case, bail early to avoid miscalculating hd_size.
  43. */
  44. if (bdev_logical_block_size(state->bdev) != 512)
  45. return 0;
  46. rs = read_part_sector(state, 0, &sect);
  47. if (!rs)
  48. return -1;
  49. /* Verify this is an Atari rootsector: */
  50. hd_size = state->bdev->bd_inode->i_size >> 9;
  51. if (!VALID_PARTITION(&rs->part[0], hd_size) &&
  52. !VALID_PARTITION(&rs->part[1], hd_size) &&
  53. !VALID_PARTITION(&rs->part[2], hd_size) &&
  54. !VALID_PARTITION(&rs->part[3], hd_size)) {
  55. /*
  56. * if there's no valid primary partition, assume that no Atari
  57. * format partition table (there's no reliable magic or the like
  58. * :-()
  59. */
  60. put_dev_sector(sect);
  61. return 0;
  62. }
  63. pi = &rs->part[0];
  64. strlcat(state->pp_buf, " AHDI", PAGE_SIZE);
  65. for (slot = 1; pi < &rs->part[4] && slot < state->limit; slot++, pi++) {
  66. struct rootsector *xrs;
  67. Sector sect2;
  68. ulong partsect;
  69. if ( !(pi->flg & 1) )
  70. continue;
  71. /* active partition */
  72. if (memcmp (pi->id, "XGM", 3) != 0) {
  73. /* we don't care about other id's */
  74. put_partition (state, slot, be32_to_cpu(pi->st),
  75. be32_to_cpu(pi->siz));
  76. continue;
  77. }
  78. /* extension partition */
  79. #ifdef ICD_PARTS
  80. part_fmt = 1;
  81. #endif
  82. strlcat(state->pp_buf, " XGM<", PAGE_SIZE);
  83. partsect = extensect = be32_to_cpu(pi->st);
  84. while (1) {
  85. xrs = read_part_sector(state, partsect, &sect2);
  86. if (!xrs) {
  87. printk (" block %ld read failed\n", partsect);
  88. put_dev_sector(sect);
  89. return -1;
  90. }
  91. /* ++roman: sanity check: bit 0 of flg field must be set */
  92. if (!(xrs->part[0].flg & 1)) {
  93. printk( "\nFirst sub-partition in extended partition is not valid!\n" );
  94. put_dev_sector(sect2);
  95. break;
  96. }
  97. put_partition(state, slot,
  98. partsect + be32_to_cpu(xrs->part[0].st),
  99. be32_to_cpu(xrs->part[0].siz));
  100. if (!(xrs->part[1].flg & 1)) {
  101. /* end of linked partition list */
  102. put_dev_sector(sect2);
  103. break;
  104. }
  105. if (memcmp( xrs->part[1].id, "XGM", 3 ) != 0) {
  106. printk("\nID of extended partition is not XGM!\n");
  107. put_dev_sector(sect2);
  108. break;
  109. }
  110. partsect = be32_to_cpu(xrs->part[1].st) + extensect;
  111. put_dev_sector(sect2);
  112. if (++slot == state->limit) {
  113. printk( "\nMaximum number of partitions reached!\n" );
  114. break;
  115. }
  116. }
  117. strlcat(state->pp_buf, " >", PAGE_SIZE);
  118. }
  119. #ifdef ICD_PARTS
  120. if ( part_fmt!=1 ) { /* no extended partitions -> test ICD-format */
  121. pi = &rs->icdpart[0];
  122. /* sanity check: no ICD format if first partition invalid */
  123. if (OK_id(pi->id)) {
  124. strlcat(state->pp_buf, " ICD<", PAGE_SIZE);
  125. for (; pi < &rs->icdpart[8] && slot < state->limit; slot++, pi++) {
  126. /* accept only GEM,BGM,RAW,LNX,SWP partitions */
  127. if (!((pi->flg & 1) && OK_id(pi->id)))
  128. continue;
  129. part_fmt = 2;
  130. put_partition (state, slot,
  131. be32_to_cpu(pi->st),
  132. be32_to_cpu(pi->siz));
  133. }
  134. strlcat(state->pp_buf, " >", PAGE_SIZE);
  135. }
  136. }
  137. #endif
  138. put_dev_sector(sect);
  139. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  140. return 1;
  141. }