acorn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * linux/fs/partitions/acorn.c
  3. *
  4. * Copyright (c) 1996-2000 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Scan ADFS partitions on hard disk drives. Unfortunately, there
  11. * isn't a standard for partitioning drives on Acorn machines, so
  12. * every single manufacturer of SCSI and IDE cards created their own
  13. * method.
  14. */
  15. #include <linux/buffer_head.h>
  16. #include <linux/adfs_fs.h>
  17. #include "check.h"
  18. #include "acorn.h"
  19. /*
  20. * Partition types. (Oh for reusability)
  21. */
  22. #define PARTITION_RISCIX_MFM 1
  23. #define PARTITION_RISCIX_SCSI 2
  24. #define PARTITION_LINUX 9
  25. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  26. defined(CONFIG_ACORN_PARTITION_ADFS)
  27. static struct adfs_discrecord *
  28. adfs_partition(struct parsed_partitions *state, char *name, char *data,
  29. unsigned long first_sector, int slot)
  30. {
  31. struct adfs_discrecord *dr;
  32. unsigned int nr_sects;
  33. if (adfs_checkbblk(data))
  34. return NULL;
  35. dr = (struct adfs_discrecord *)(data + 0x1c0);
  36. if (dr->disc_size == 0 && dr->disc_size_high == 0)
  37. return NULL;
  38. nr_sects = (le32_to_cpu(dr->disc_size_high) << 23) |
  39. (le32_to_cpu(dr->disc_size) >> 9);
  40. if (name)
  41. printk(" [%s]", name);
  42. put_partition(state, slot, first_sector, nr_sects);
  43. return dr;
  44. }
  45. #endif
  46. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  47. struct riscix_part {
  48. __le32 start;
  49. __le32 length;
  50. __le32 one;
  51. char name[16];
  52. };
  53. struct riscix_record {
  54. __le32 magic;
  55. #define RISCIX_MAGIC cpu_to_le32(0x4a657320)
  56. __le32 date;
  57. struct riscix_part part[8];
  58. };
  59. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  60. defined(CONFIG_ACORN_PARTITION_ADFS)
  61. static int
  62. riscix_partition(struct parsed_partitions *state, struct block_device *bdev,
  63. unsigned long first_sect, int slot, unsigned long nr_sects)
  64. {
  65. Sector sect;
  66. struct riscix_record *rr;
  67. rr = (struct riscix_record *)read_dev_sector(bdev, first_sect, &sect);
  68. if (!rr)
  69. return -1;
  70. printk(" [RISCiX]");
  71. if (rr->magic == RISCIX_MAGIC) {
  72. unsigned long size = nr_sects > 2 ? 2 : nr_sects;
  73. int part;
  74. printk(" <");
  75. put_partition(state, slot++, first_sect, size);
  76. for (part = 0; part < 8; part++) {
  77. if (rr->part[part].one &&
  78. memcmp(rr->part[part].name, "All\0", 4)) {
  79. put_partition(state, slot++,
  80. le32_to_cpu(rr->part[part].start),
  81. le32_to_cpu(rr->part[part].length));
  82. printk("(%s)", rr->part[part].name);
  83. }
  84. }
  85. printk(" >\n");
  86. } else {
  87. put_partition(state, slot++, first_sect, nr_sects);
  88. }
  89. put_dev_sector(sect);
  90. return slot;
  91. }
  92. #endif
  93. #endif
  94. #define LINUX_NATIVE_MAGIC 0xdeafa1de
  95. #define LINUX_SWAP_MAGIC 0xdeafab1e
  96. struct linux_part {
  97. __le32 magic;
  98. __le32 start_sect;
  99. __le32 nr_sects;
  100. };
  101. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  102. defined(CONFIG_ACORN_PARTITION_ADFS)
  103. static int
  104. linux_partition(struct parsed_partitions *state, struct block_device *bdev,
  105. unsigned long first_sect, int slot, unsigned long nr_sects)
  106. {
  107. Sector sect;
  108. struct linux_part *linuxp;
  109. unsigned long size = nr_sects > 2 ? 2 : nr_sects;
  110. printk(" [Linux]");
  111. put_partition(state, slot++, first_sect, size);
  112. linuxp = (struct linux_part *)read_dev_sector(bdev, first_sect, &sect);
  113. if (!linuxp)
  114. return -1;
  115. printk(" <");
  116. while (linuxp->magic == cpu_to_le32(LINUX_NATIVE_MAGIC) ||
  117. linuxp->magic == cpu_to_le32(LINUX_SWAP_MAGIC)) {
  118. if (slot == state->limit)
  119. break;
  120. put_partition(state, slot++, first_sect +
  121. le32_to_cpu(linuxp->start_sect),
  122. le32_to_cpu(linuxp->nr_sects));
  123. linuxp ++;
  124. }
  125. printk(" >");
  126. put_dev_sector(sect);
  127. return slot;
  128. }
  129. #endif
  130. #ifdef CONFIG_ACORN_PARTITION_CUMANA
  131. int
  132. adfspart_check_CUMANA(struct parsed_partitions *state, struct block_device *bdev)
  133. {
  134. unsigned long first_sector = 0;
  135. unsigned int start_blk = 0;
  136. Sector sect;
  137. unsigned char *data;
  138. char *name = "CUMANA/ADFS";
  139. int first = 1;
  140. int slot = 1;
  141. /*
  142. * Try Cumana style partitions - sector 6 contains ADFS boot block
  143. * with pointer to next 'drive'.
  144. *
  145. * There are unknowns in this code - is the 'cylinder number' of the
  146. * next partition relative to the start of this one - I'm assuming
  147. * it is.
  148. *
  149. * Also, which ID did Cumana use?
  150. *
  151. * This is totally unfinished, and will require more work to get it
  152. * going. Hence it is totally untested.
  153. */
  154. do {
  155. struct adfs_discrecord *dr;
  156. unsigned int nr_sects;
  157. data = read_dev_sector(bdev, start_blk * 2 + 6, &sect);
  158. if (!data)
  159. return -1;
  160. if (slot == state->limit)
  161. break;
  162. dr = adfs_partition(state, name, data, first_sector, slot++);
  163. if (!dr)
  164. break;
  165. name = NULL;
  166. nr_sects = (data[0x1fd] + (data[0x1fe] << 8)) *
  167. (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) *
  168. dr->secspertrack;
  169. if (!nr_sects)
  170. break;
  171. first = 0;
  172. first_sector += nr_sects;
  173. start_blk += nr_sects >> (BLOCK_SIZE_BITS - 9);
  174. nr_sects = 0; /* hmm - should be partition size */
  175. switch (data[0x1fc] & 15) {
  176. case 0: /* No partition / ADFS? */
  177. break;
  178. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  179. case PARTITION_RISCIX_SCSI:
  180. /* RISCiX - we don't know how to find the next one. */
  181. slot = riscix_partition(state, bdev, first_sector,
  182. slot, nr_sects);
  183. break;
  184. #endif
  185. case PARTITION_LINUX:
  186. slot = linux_partition(state, bdev, first_sector,
  187. slot, nr_sects);
  188. break;
  189. }
  190. put_dev_sector(sect);
  191. if (slot == -1)
  192. return -1;
  193. } while (1);
  194. put_dev_sector(sect);
  195. return first ? 0 : 1;
  196. }
  197. #endif
  198. #ifdef CONFIG_ACORN_PARTITION_ADFS
  199. /*
  200. * Purpose: allocate ADFS partitions.
  201. *
  202. * Params : hd - pointer to gendisk structure to store partition info.
  203. * dev - device number to access.
  204. *
  205. * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
  206. *
  207. * Alloc : hda = whole drive
  208. * hda1 = ADFS partition on first drive.
  209. * hda2 = non-ADFS partition.
  210. */
  211. int
  212. adfspart_check_ADFS(struct parsed_partitions *state, struct block_device *bdev)
  213. {
  214. unsigned long start_sect, nr_sects, sectscyl, heads;
  215. Sector sect;
  216. unsigned char *data;
  217. struct adfs_discrecord *dr;
  218. unsigned char id;
  219. int slot = 1;
  220. data = read_dev_sector(bdev, 6, &sect);
  221. if (!data)
  222. return -1;
  223. dr = adfs_partition(state, "ADFS", data, 0, slot++);
  224. if (!dr) {
  225. put_dev_sector(sect);
  226. return 0;
  227. }
  228. heads = dr->heads + ((dr->lowsector >> 6) & 1);
  229. sectscyl = dr->secspertrack * heads;
  230. start_sect = ((data[0x1fe] << 8) + data[0x1fd]) * sectscyl;
  231. id = data[0x1fc] & 15;
  232. put_dev_sector(sect);
  233. #ifdef CONFIG_BLK_DEV_MFM
  234. if (MAJOR(bdev->bd_dev) == MFM_ACORN_MAJOR) {
  235. extern void xd_set_geometry(struct block_device *,
  236. unsigned char, unsigned char, unsigned int);
  237. xd_set_geometry(bdev, dr->secspertrack, heads, 1);
  238. invalidate_bh_lrus();
  239. truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
  240. }
  241. #endif
  242. /*
  243. * Work out start of non-adfs partition.
  244. */
  245. nr_sects = (bdev->bd_inode->i_size >> 9) - start_sect;
  246. if (start_sect) {
  247. switch (id) {
  248. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  249. case PARTITION_RISCIX_SCSI:
  250. case PARTITION_RISCIX_MFM:
  251. slot = riscix_partition(state, bdev, start_sect,
  252. slot, nr_sects);
  253. break;
  254. #endif
  255. case PARTITION_LINUX:
  256. slot = linux_partition(state, bdev, start_sect,
  257. slot, nr_sects);
  258. break;
  259. }
  260. }
  261. printk("\n");
  262. return 1;
  263. }
  264. #endif
  265. #ifdef CONFIG_ACORN_PARTITION_ICS
  266. struct ics_part {
  267. __le32 start;
  268. __le32 size;
  269. };
  270. static int adfspart_check_ICSLinux(struct block_device *bdev, unsigned long block)
  271. {
  272. Sector sect;
  273. unsigned char *data = read_dev_sector(bdev, block, &sect);
  274. int result = 0;
  275. if (data) {
  276. if (memcmp(data, "LinuxPart", 9) == 0)
  277. result = 1;
  278. put_dev_sector(sect);
  279. }
  280. return result;
  281. }
  282. /*
  283. * Check for a valid ICS partition using the checksum.
  284. */
  285. static inline int valid_ics_sector(const unsigned char *data)
  286. {
  287. unsigned long sum;
  288. int i;
  289. for (i = 0, sum = 0x50617274; i < 508; i++)
  290. sum += data[i];
  291. sum -= le32_to_cpu(*(__le32 *)(&data[508]));
  292. return sum == 0;
  293. }
  294. /*
  295. * Purpose: allocate ICS partitions.
  296. * Params : hd - pointer to gendisk structure to store partition info.
  297. * dev - device number to access.
  298. * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
  299. * Alloc : hda = whole drive
  300. * hda1 = ADFS partition 0 on first drive.
  301. * hda2 = ADFS partition 1 on first drive.
  302. * ..etc..
  303. */
  304. int
  305. adfspart_check_ICS(struct parsed_partitions *state, struct block_device *bdev)
  306. {
  307. const unsigned char *data;
  308. const struct ics_part *p;
  309. int slot;
  310. Sector sect;
  311. /*
  312. * Try ICS style partitions - sector 0 contains partition info.
  313. */
  314. data = read_dev_sector(bdev, 0, &sect);
  315. if (!data)
  316. return -1;
  317. if (!valid_ics_sector(data)) {
  318. put_dev_sector(sect);
  319. return 0;
  320. }
  321. printk(" [ICS]");
  322. for (slot = 1, p = (const struct ics_part *)data; p->size; p++) {
  323. u32 start = le32_to_cpu(p->start);
  324. s32 size = le32_to_cpu(p->size); /* yes, it's signed. */
  325. if (slot == state->limit)
  326. break;
  327. /*
  328. * Negative sizes tell the RISC OS ICS driver to ignore
  329. * this partition - in effect it says that this does not
  330. * contain an ADFS filesystem.
  331. */
  332. if (size < 0) {
  333. size = -size;
  334. /*
  335. * Our own extension - We use the first sector
  336. * of the partition to identify what type this
  337. * partition is. We must not make this visible
  338. * to the filesystem.
  339. */
  340. if (size > 1 && adfspart_check_ICSLinux(bdev, start)) {
  341. start += 1;
  342. size -= 1;
  343. }
  344. }
  345. if (size)
  346. put_partition(state, slot++, start, size);
  347. }
  348. put_dev_sector(sect);
  349. printk("\n");
  350. return 1;
  351. }
  352. #endif
  353. #ifdef CONFIG_ACORN_PARTITION_POWERTEC
  354. struct ptec_part {
  355. __le32 unused1;
  356. __le32 unused2;
  357. __le32 start;
  358. __le32 size;
  359. __le32 unused5;
  360. char type[8];
  361. };
  362. static inline int valid_ptec_sector(const unsigned char *data)
  363. {
  364. unsigned char checksum = 0x2a;
  365. int i;
  366. /*
  367. * If it looks like a PC/BIOS partition, then it
  368. * probably isn't PowerTec.
  369. */
  370. if (data[510] == 0x55 && data[511] == 0xaa)
  371. return 0;
  372. for (i = 0; i < 511; i++)
  373. checksum += data[i];
  374. return checksum == data[511];
  375. }
  376. /*
  377. * Purpose: allocate ICS partitions.
  378. * Params : hd - pointer to gendisk structure to store partition info.
  379. * dev - device number to access.
  380. * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
  381. * Alloc : hda = whole drive
  382. * hda1 = ADFS partition 0 on first drive.
  383. * hda2 = ADFS partition 1 on first drive.
  384. * ..etc..
  385. */
  386. int
  387. adfspart_check_POWERTEC(struct parsed_partitions *state, struct block_device *bdev)
  388. {
  389. Sector sect;
  390. const unsigned char *data;
  391. const struct ptec_part *p;
  392. int slot = 1;
  393. int i;
  394. data = read_dev_sector(bdev, 0, &sect);
  395. if (!data)
  396. return -1;
  397. if (!valid_ptec_sector(data)) {
  398. put_dev_sector(sect);
  399. return 0;
  400. }
  401. printk(" [POWERTEC]");
  402. for (i = 0, p = (const struct ptec_part *)data; i < 12; i++, p++) {
  403. u32 start = le32_to_cpu(p->start);
  404. u32 size = le32_to_cpu(p->size);
  405. if (size)
  406. put_partition(state, slot++, start, size);
  407. }
  408. put_dev_sector(sect);
  409. printk("\n");
  410. return 1;
  411. }
  412. #endif
  413. #ifdef CONFIG_ACORN_PARTITION_EESOX
  414. struct eesox_part {
  415. char magic[6];
  416. char name[10];
  417. __le32 start;
  418. __le32 unused6;
  419. __le32 unused7;
  420. __le32 unused8;
  421. };
  422. /*
  423. * Guess who created this format?
  424. */
  425. static const char eesox_name[] = {
  426. 'N', 'e', 'i', 'l', ' ',
  427. 'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
  428. };
  429. /*
  430. * EESOX SCSI partition format.
  431. *
  432. * This is a goddamned awful partition format. We don't seem to store
  433. * the size of the partition in this table, only the start addresses.
  434. *
  435. * There are two possibilities where the size comes from:
  436. * 1. The individual ADFS boot block entries that are placed on the disk.
  437. * 2. The start address of the next entry.
  438. */
  439. int
  440. adfspart_check_EESOX(struct parsed_partitions *state, struct block_device *bdev)
  441. {
  442. Sector sect;
  443. const unsigned char *data;
  444. unsigned char buffer[256];
  445. struct eesox_part *p;
  446. sector_t start = 0;
  447. int i, slot = 1;
  448. data = read_dev_sector(bdev, 7, &sect);
  449. if (!data)
  450. return -1;
  451. /*
  452. * "Decrypt" the partition table. God knows why...
  453. */
  454. for (i = 0; i < 256; i++)
  455. buffer[i] = data[i] ^ eesox_name[i & 15];
  456. put_dev_sector(sect);
  457. for (i = 0, p = (struct eesox_part *)buffer; i < 8; i++, p++) {
  458. sector_t next;
  459. if (memcmp(p->magic, "Eesox", 6))
  460. break;
  461. next = le32_to_cpu(p->start);
  462. if (i)
  463. put_partition(state, slot++, start, next - start);
  464. start = next;
  465. }
  466. if (i != 0) {
  467. sector_t size;
  468. size = get_capacity(bdev->bd_disk);
  469. put_partition(state, slot++, start, size - start);
  470. printk("\n");
  471. }
  472. return i ? 1 : 0;
  473. }
  474. #endif