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