acorn.c 12 KB

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