ldm.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /**
  2. * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
  3. *
  4. * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
  5. * Copyright (c) 2001-2007 Anton Altaparmakov
  6. * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
  7. *
  8. * Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program (in the main directory of the source in the file COPYING); if
  22. * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  23. * Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/stringify.h>
  28. #include <linux/kernel.h>
  29. #include "ldm.h"
  30. #include "check.h"
  31. #include "msdos.h"
  32. /**
  33. * ldm_debug/info/error/crit - Output an error message
  34. * @f: A printf format string containing the message
  35. * @...: Variables to substitute into @f
  36. *
  37. * ldm_debug() writes a DEBUG level message to the syslog but only if the
  38. * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
  39. */
  40. #ifndef CONFIG_LDM_DEBUG
  41. #define ldm_debug(...) do {} while (0)
  42. #else
  43. #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
  44. #endif
  45. #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
  46. #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
  47. #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
  48. __attribute__ ((format (printf, 3, 4)))
  49. static void _ldm_printk (const char *level, const char *function,
  50. const char *fmt, ...)
  51. {
  52. static char buf[128];
  53. va_list args;
  54. va_start (args, fmt);
  55. vsnprintf (buf, sizeof (buf), fmt, args);
  56. va_end (args);
  57. printk ("%s%s(): %s\n", level, function, buf);
  58. }
  59. /**
  60. * ldm_parse_hexbyte - Convert a ASCII hex number to a byte
  61. * @src: Pointer to at least 2 characters to convert.
  62. *
  63. * Convert a two character ASCII hex string to a number.
  64. *
  65. * Return: 0-255 Success, the byte was parsed correctly
  66. * -1 Error, an invalid character was supplied
  67. */
  68. static int ldm_parse_hexbyte (const u8 *src)
  69. {
  70. unsigned int x; /* For correct wrapping */
  71. int h;
  72. /* high part */
  73. x = h = hex_to_bin(src[0]);
  74. if (h < 0)
  75. return -1;
  76. /* low part */
  77. h = hex_to_bin(src[1]);
  78. if (h < 0)
  79. return -1;
  80. return (x << 4) + h;
  81. }
  82. /**
  83. * ldm_parse_guid - Convert GUID from ASCII to binary
  84. * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
  85. * @dest: Memory block to hold binary GUID (16 bytes)
  86. *
  87. * N.B. The GUID need not be NULL terminated.
  88. *
  89. * Return: 'true' @dest contains binary GUID
  90. * 'false' @dest contents are undefined
  91. */
  92. static bool ldm_parse_guid (const u8 *src, u8 *dest)
  93. {
  94. static const int size[] = { 4, 2, 2, 2, 6 };
  95. int i, j, v;
  96. if (src[8] != '-' || src[13] != '-' ||
  97. src[18] != '-' || src[23] != '-')
  98. return false;
  99. for (j = 0; j < 5; j++, src++)
  100. for (i = 0; i < size[j]; i++, src+=2, *dest++ = v)
  101. if ((v = ldm_parse_hexbyte (src)) < 0)
  102. return false;
  103. return true;
  104. }
  105. /**
  106. * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
  107. * @data: Raw database PRIVHEAD structure loaded from the device
  108. * @ph: In-memory privhead structure in which to return parsed information
  109. *
  110. * This parses the LDM database PRIVHEAD structure supplied in @data and
  111. * sets up the in-memory privhead structure @ph with the obtained information.
  112. *
  113. * Return: 'true' @ph contains the PRIVHEAD data
  114. * 'false' @ph contents are undefined
  115. */
  116. static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
  117. {
  118. bool is_vista = false;
  119. BUG_ON(!data || !ph);
  120. if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
  121. ldm_error("Cannot find PRIVHEAD structure. LDM database is"
  122. " corrupt. Aborting.");
  123. return false;
  124. }
  125. ph->ver_major = get_unaligned_be16(data + 0x000C);
  126. ph->ver_minor = get_unaligned_be16(data + 0x000E);
  127. ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
  128. ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
  129. ph->config_start = get_unaligned_be64(data + 0x012B);
  130. ph->config_size = get_unaligned_be64(data + 0x0133);
  131. /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
  132. if (ph->ver_major == 2 && ph->ver_minor == 12)
  133. is_vista = true;
  134. if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
  135. ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
  136. " Aborting.", ph->ver_major, ph->ver_minor);
  137. return false;
  138. }
  139. ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
  140. ph->ver_minor, is_vista ? "Vista" : "2000/XP");
  141. if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
  142. /* Warn the user and continue, carefully. */
  143. ldm_info("Database is normally %u bytes, it claims to "
  144. "be %llu bytes.", LDM_DB_SIZE,
  145. (unsigned long long)ph->config_size);
  146. }
  147. if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
  148. ph->logical_disk_size > ph->config_start)) {
  149. ldm_error("PRIVHEAD disk size doesn't match real disk size");
  150. return false;
  151. }
  152. if (!ldm_parse_guid(data + 0x0030, ph->disk_id)) {
  153. ldm_error("PRIVHEAD contains an invalid GUID.");
  154. return false;
  155. }
  156. ldm_debug("Parsed PRIVHEAD successfully.");
  157. return true;
  158. }
  159. /**
  160. * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
  161. * @data: Raw database TOCBLOCK structure loaded from the device
  162. * @toc: In-memory toc structure in which to return parsed information
  163. *
  164. * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
  165. * in @data and sets up the in-memory tocblock structure @toc with the obtained
  166. * information.
  167. *
  168. * N.B. The *_start and *_size values returned in @toc are not range-checked.
  169. *
  170. * Return: 'true' @toc contains the TOCBLOCK data
  171. * 'false' @toc contents are undefined
  172. */
  173. static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
  174. {
  175. BUG_ON (!data || !toc);
  176. if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
  177. ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
  178. return false;
  179. }
  180. strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
  181. toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
  182. toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
  183. toc->bitmap1_size = get_unaligned_be64(data + 0x36);
  184. if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
  185. sizeof (toc->bitmap1_name)) != 0) {
  186. ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
  187. TOC_BITMAP1, toc->bitmap1_name);
  188. return false;
  189. }
  190. strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
  191. toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
  192. toc->bitmap2_start = get_unaligned_be64(data + 0x50);
  193. toc->bitmap2_size = get_unaligned_be64(data + 0x58);
  194. if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
  195. sizeof (toc->bitmap2_name)) != 0) {
  196. ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
  197. TOC_BITMAP2, toc->bitmap2_name);
  198. return false;
  199. }
  200. ldm_debug ("Parsed TOCBLOCK successfully.");
  201. return true;
  202. }
  203. /**
  204. * ldm_parse_vmdb - Read the LDM Database VMDB structure
  205. * @data: Raw database VMDB structure loaded from the device
  206. * @vm: In-memory vmdb structure in which to return parsed information
  207. *
  208. * This parses the LDM Database VMDB structure supplied in @data and sets up
  209. * the in-memory vmdb structure @vm with the obtained information.
  210. *
  211. * N.B. The *_start, *_size and *_seq values will be range-checked later.
  212. *
  213. * Return: 'true' @vm contains VMDB info
  214. * 'false' @vm contents are undefined
  215. */
  216. static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
  217. {
  218. BUG_ON (!data || !vm);
  219. if (MAGIC_VMDB != get_unaligned_be32(data)) {
  220. ldm_crit ("Cannot find the VMDB, database may be corrupt.");
  221. return false;
  222. }
  223. vm->ver_major = get_unaligned_be16(data + 0x12);
  224. vm->ver_minor = get_unaligned_be16(data + 0x14);
  225. if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
  226. ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
  227. "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
  228. return false;
  229. }
  230. vm->vblk_size = get_unaligned_be32(data + 0x08);
  231. if (vm->vblk_size == 0) {
  232. ldm_error ("Illegal VBLK size");
  233. return false;
  234. }
  235. vm->vblk_offset = get_unaligned_be32(data + 0x0C);
  236. vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
  237. ldm_debug ("Parsed VMDB successfully.");
  238. return true;
  239. }
  240. /**
  241. * ldm_compare_privheads - Compare two privhead objects
  242. * @ph1: First privhead
  243. * @ph2: Second privhead
  244. *
  245. * This compares the two privhead structures @ph1 and @ph2.
  246. *
  247. * Return: 'true' Identical
  248. * 'false' Different
  249. */
  250. static bool ldm_compare_privheads (const struct privhead *ph1,
  251. const struct privhead *ph2)
  252. {
  253. BUG_ON (!ph1 || !ph2);
  254. return ((ph1->ver_major == ph2->ver_major) &&
  255. (ph1->ver_minor == ph2->ver_minor) &&
  256. (ph1->logical_disk_start == ph2->logical_disk_start) &&
  257. (ph1->logical_disk_size == ph2->logical_disk_size) &&
  258. (ph1->config_start == ph2->config_start) &&
  259. (ph1->config_size == ph2->config_size) &&
  260. !memcmp (ph1->disk_id, ph2->disk_id, GUID_SIZE));
  261. }
  262. /**
  263. * ldm_compare_tocblocks - Compare two tocblock objects
  264. * @toc1: First toc
  265. * @toc2: Second toc
  266. *
  267. * This compares the two tocblock structures @toc1 and @toc2.
  268. *
  269. * Return: 'true' Identical
  270. * 'false' Different
  271. */
  272. static bool ldm_compare_tocblocks (const struct tocblock *toc1,
  273. const struct tocblock *toc2)
  274. {
  275. BUG_ON (!toc1 || !toc2);
  276. return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
  277. (toc1->bitmap1_size == toc2->bitmap1_size) &&
  278. (toc1->bitmap2_start == toc2->bitmap2_start) &&
  279. (toc1->bitmap2_size == toc2->bitmap2_size) &&
  280. !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
  281. sizeof (toc1->bitmap1_name)) &&
  282. !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
  283. sizeof (toc1->bitmap2_name)));
  284. }
  285. /**
  286. * ldm_validate_privheads - Compare the primary privhead with its backups
  287. * @state: Partition check state including device holding the LDM Database
  288. * @ph1: Memory struct to fill with ph contents
  289. *
  290. * Read and compare all three privheads from disk.
  291. *
  292. * The privheads on disk show the size and location of the main disk area and
  293. * the configuration area (the database). The values are range-checked against
  294. * @hd, which contains the real size of the disk.
  295. *
  296. * Return: 'true' Success
  297. * 'false' Error
  298. */
  299. static bool ldm_validate_privheads(struct parsed_partitions *state,
  300. struct privhead *ph1)
  301. {
  302. static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
  303. struct privhead *ph[3] = { ph1 };
  304. Sector sect;
  305. u8 *data;
  306. bool result = false;
  307. long num_sects;
  308. int i;
  309. BUG_ON (!state || !ph1);
  310. ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
  311. ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
  312. if (!ph[1] || !ph[2]) {
  313. ldm_crit ("Out of memory.");
  314. goto out;
  315. }
  316. /* off[1 & 2] are relative to ph[0]->config_start */
  317. ph[0]->config_start = 0;
  318. /* Read and parse privheads */
  319. for (i = 0; i < 3; i++) {
  320. data = read_part_sector(state, ph[0]->config_start + off[i],
  321. &sect);
  322. if (!data) {
  323. ldm_crit ("Disk read failed.");
  324. goto out;
  325. }
  326. result = ldm_parse_privhead (data, ph[i]);
  327. put_dev_sector (sect);
  328. if (!result) {
  329. ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
  330. if (i < 2)
  331. goto out; /* Already logged */
  332. else
  333. break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
  334. }
  335. }
  336. num_sects = state->bdev->bd_inode->i_size >> 9;
  337. if ((ph[0]->config_start > num_sects) ||
  338. ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
  339. ldm_crit ("Database extends beyond the end of the disk.");
  340. goto out;
  341. }
  342. if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
  343. ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
  344. > ph[0]->config_start)) {
  345. ldm_crit ("Disk and database overlap.");
  346. goto out;
  347. }
  348. if (!ldm_compare_privheads (ph[0], ph[1])) {
  349. ldm_crit ("Primary and backup PRIVHEADs don't match.");
  350. goto out;
  351. }
  352. /* FIXME ignore this for now
  353. if (!ldm_compare_privheads (ph[0], ph[2])) {
  354. ldm_crit ("Primary and backup PRIVHEADs don't match.");
  355. goto out;
  356. }*/
  357. ldm_debug ("Validated PRIVHEADs successfully.");
  358. result = true;
  359. out:
  360. kfree (ph[1]);
  361. kfree (ph[2]);
  362. return result;
  363. }
  364. /**
  365. * ldm_validate_tocblocks - Validate the table of contents and its backups
  366. * @state: Partition check state including device holding the LDM Database
  367. * @base: Offset, into @state->bdev, of the database
  368. * @ldb: Cache of the database structures
  369. *
  370. * Find and compare the four tables of contents of the LDM Database stored on
  371. * @state->bdev and return the parsed information into @toc1.
  372. *
  373. * The offsets and sizes of the configs are range-checked against a privhead.
  374. *
  375. * Return: 'true' @toc1 contains validated TOCBLOCK info
  376. * 'false' @toc1 contents are undefined
  377. */
  378. static bool ldm_validate_tocblocks(struct parsed_partitions *state,
  379. unsigned long base, struct ldmdb *ldb)
  380. {
  381. static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
  382. struct tocblock *tb[4];
  383. struct privhead *ph;
  384. Sector sect;
  385. u8 *data;
  386. int i, nr_tbs;
  387. bool result = false;
  388. BUG_ON(!state || !ldb);
  389. ph = &ldb->ph;
  390. tb[0] = &ldb->toc;
  391. tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL);
  392. if (!tb[1]) {
  393. ldm_crit("Out of memory.");
  394. goto err;
  395. }
  396. tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
  397. tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
  398. /*
  399. * Try to read and parse all four TOCBLOCKs.
  400. *
  401. * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
  402. * skip any that fail as long as we get at least one valid TOCBLOCK.
  403. */
  404. for (nr_tbs = i = 0; i < 4; i++) {
  405. data = read_part_sector(state, base + off[i], &sect);
  406. if (!data) {
  407. ldm_error("Disk read failed for TOCBLOCK %d.", i);
  408. continue;
  409. }
  410. if (ldm_parse_tocblock(data, tb[nr_tbs]))
  411. nr_tbs++;
  412. put_dev_sector(sect);
  413. }
  414. if (!nr_tbs) {
  415. ldm_crit("Failed to find a valid TOCBLOCK.");
  416. goto err;
  417. }
  418. /* Range check the TOCBLOCK against a privhead. */
  419. if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
  420. ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
  421. ph->config_size)) {
  422. ldm_crit("The bitmaps are out of range. Giving up.");
  423. goto err;
  424. }
  425. /* Compare all loaded TOCBLOCKs. */
  426. for (i = 1; i < nr_tbs; i++) {
  427. if (!ldm_compare_tocblocks(tb[0], tb[i])) {
  428. ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
  429. goto err;
  430. }
  431. }
  432. ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
  433. result = true;
  434. err:
  435. kfree(tb[1]);
  436. return result;
  437. }
  438. /**
  439. * ldm_validate_vmdb - Read the VMDB and validate it
  440. * @state: Partition check state including device holding the LDM Database
  441. * @base: Offset, into @bdev, of the database
  442. * @ldb: Cache of the database structures
  443. *
  444. * Find the vmdb of the LDM Database stored on @bdev and return the parsed
  445. * information in @ldb.
  446. *
  447. * Return: 'true' @ldb contains validated VBDB info
  448. * 'false' @ldb contents are undefined
  449. */
  450. static bool ldm_validate_vmdb(struct parsed_partitions *state,
  451. unsigned long base, struct ldmdb *ldb)
  452. {
  453. Sector sect;
  454. u8 *data;
  455. bool result = false;
  456. struct vmdb *vm;
  457. struct tocblock *toc;
  458. BUG_ON (!state || !ldb);
  459. vm = &ldb->vm;
  460. toc = &ldb->toc;
  461. data = read_part_sector(state, base + OFF_VMDB, &sect);
  462. if (!data) {
  463. ldm_crit ("Disk read failed.");
  464. return false;
  465. }
  466. if (!ldm_parse_vmdb (data, vm))
  467. goto out; /* Already logged */
  468. /* Are there uncommitted transactions? */
  469. if (get_unaligned_be16(data + 0x10) != 0x01) {
  470. ldm_crit ("Database is not in a consistent state. Aborting.");
  471. goto out;
  472. }
  473. if (vm->vblk_offset != 512)
  474. ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
  475. /*
  476. * The last_vblkd_seq can be before the end of the vmdb, just make sure
  477. * it is not out of bounds.
  478. */
  479. if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
  480. ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
  481. "Database is corrupt. Aborting.");
  482. goto out;
  483. }
  484. result = true;
  485. out:
  486. put_dev_sector (sect);
  487. return result;
  488. }
  489. /**
  490. * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
  491. * @state: Partition check state including device holding the LDM Database
  492. *
  493. * This function provides a weak test to decide whether the device is a dynamic
  494. * disk or not. It looks for an MS-DOS-style partition table containing at
  495. * least one partition of type 0x42 (formerly SFS, now used by Windows for
  496. * dynamic disks).
  497. *
  498. * N.B. The only possible error can come from the read_part_sector and that is
  499. * only likely to happen if the underlying device is strange. If that IS
  500. * the case we should return zero to let someone else try.
  501. *
  502. * Return: 'true' @state->bdev is a dynamic disk
  503. * 'false' @state->bdev is not a dynamic disk, or an error occurred
  504. */
  505. static bool ldm_validate_partition_table(struct parsed_partitions *state)
  506. {
  507. Sector sect;
  508. u8 *data;
  509. struct partition *p;
  510. int i;
  511. bool result = false;
  512. BUG_ON(!state);
  513. data = read_part_sector(state, 0, &sect);
  514. if (!data) {
  515. ldm_info ("Disk read failed.");
  516. return false;
  517. }
  518. if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
  519. goto out;
  520. p = (struct partition*)(data + 0x01BE);
  521. for (i = 0; i < 4; i++, p++)
  522. if (SYS_IND (p) == LDM_PARTITION) {
  523. result = true;
  524. break;
  525. }
  526. if (result)
  527. ldm_debug ("Found W2K dynamic disk partition type.");
  528. out:
  529. put_dev_sector (sect);
  530. return result;
  531. }
  532. /**
  533. * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
  534. * @ldb: Cache of the database structures
  535. *
  536. * The LDM Database contains a list of all partitions on all dynamic disks.
  537. * The primary PRIVHEAD, at the beginning of the physical disk, tells us
  538. * the GUID of this disk. This function searches for the GUID in a linked
  539. * list of vblk's.
  540. *
  541. * Return: Pointer, A matching vblk was found
  542. * NULL, No match, or an error
  543. */
  544. static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
  545. {
  546. struct list_head *item;
  547. BUG_ON (!ldb);
  548. list_for_each (item, &ldb->v_disk) {
  549. struct vblk *v = list_entry (item, struct vblk, list);
  550. if (!memcmp (v->vblk.disk.disk_id, ldb->ph.disk_id, GUID_SIZE))
  551. return v;
  552. }
  553. return NULL;
  554. }
  555. /**
  556. * ldm_create_data_partitions - Create data partitions for this device
  557. * @pp: List of the partitions parsed so far
  558. * @ldb: Cache of the database structures
  559. *
  560. * The database contains ALL the partitions for ALL disk groups, so we need to
  561. * filter out this specific disk. Using the disk's object id, we can find all
  562. * the partitions in the database that belong to this disk.
  563. *
  564. * Add each partition in our database, to the parsed_partitions structure.
  565. *
  566. * N.B. This function creates the partitions in the order it finds partition
  567. * objects in the linked list.
  568. *
  569. * Return: 'true' Partition created
  570. * 'false' Error, probably a range checking problem
  571. */
  572. static bool ldm_create_data_partitions (struct parsed_partitions *pp,
  573. const struct ldmdb *ldb)
  574. {
  575. struct list_head *item;
  576. struct vblk *vb;
  577. struct vblk *disk;
  578. struct vblk_part *part;
  579. int part_num = 1;
  580. BUG_ON (!pp || !ldb);
  581. disk = ldm_get_disk_objid (ldb);
  582. if (!disk) {
  583. ldm_crit ("Can't find the ID of this disk in the database.");
  584. return false;
  585. }
  586. strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
  587. /* Create the data partitions */
  588. list_for_each (item, &ldb->v_part) {
  589. vb = list_entry (item, struct vblk, list);
  590. part = &vb->vblk.part;
  591. if (part->disk_id != disk->obj_id)
  592. continue;
  593. put_partition (pp, part_num, ldb->ph.logical_disk_start +
  594. part->start, part->size);
  595. part_num++;
  596. }
  597. strlcat(pp->pp_buf, "\n", PAGE_SIZE);
  598. return true;
  599. }
  600. /**
  601. * ldm_relative - Calculate the next relative offset
  602. * @buffer: Block of data being worked on
  603. * @buflen: Size of the block of data
  604. * @base: Size of the previous fixed width fields
  605. * @offset: Cumulative size of the previous variable-width fields
  606. *
  607. * Because many of the VBLK fields are variable-width, it's necessary
  608. * to calculate each offset based on the previous one and the length
  609. * of the field it pointed to.
  610. *
  611. * Return: -1 Error, the calculated offset exceeded the size of the buffer
  612. * n OK, a range-checked offset into buffer
  613. */
  614. static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
  615. {
  616. base += offset;
  617. if (!buffer || offset < 0 || base > buflen) {
  618. if (!buffer)
  619. ldm_error("!buffer");
  620. if (offset < 0)
  621. ldm_error("offset (%d) < 0", offset);
  622. if (base > buflen)
  623. ldm_error("base (%d) > buflen (%d)", base, buflen);
  624. return -1;
  625. }
  626. if (base + buffer[base] >= buflen) {
  627. ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
  628. buffer[base], buflen);
  629. return -1;
  630. }
  631. return buffer[base] + offset + 1;
  632. }
  633. /**
  634. * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
  635. * @block: Pointer to the variable-width number to convert
  636. *
  637. * Large numbers in the LDM Database are often stored in a packed format. Each
  638. * number is prefixed by a one byte width marker. All numbers in the database
  639. * are stored in big-endian byte order. This function reads one of these
  640. * numbers and returns the result
  641. *
  642. * N.B. This function DOES NOT perform any range checking, though the most
  643. * it will read is eight bytes.
  644. *
  645. * Return: n A number
  646. * 0 Zero, or an error occurred
  647. */
  648. static u64 ldm_get_vnum (const u8 *block)
  649. {
  650. u64 tmp = 0;
  651. u8 length;
  652. BUG_ON (!block);
  653. length = *block++;
  654. if (length && length <= 8)
  655. while (length--)
  656. tmp = (tmp << 8) | *block++;
  657. else
  658. ldm_error ("Illegal length %d.", length);
  659. return tmp;
  660. }
  661. /**
  662. * ldm_get_vstr - Read a length-prefixed string into a buffer
  663. * @block: Pointer to the length marker
  664. * @buffer: Location to copy string to
  665. * @buflen: Size of the output buffer
  666. *
  667. * Many of the strings in the LDM Database are not NULL terminated. Instead
  668. * they are prefixed by a one byte length marker. This function copies one of
  669. * these strings into a buffer.
  670. *
  671. * N.B. This function DOES NOT perform any range checking on the input.
  672. * If the buffer is too small, the output will be truncated.
  673. *
  674. * Return: 0, Error and @buffer contents are undefined
  675. * n, String length in characters (excluding NULL)
  676. * buflen-1, String was truncated.
  677. */
  678. static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
  679. {
  680. int length;
  681. BUG_ON (!block || !buffer);
  682. length = block[0];
  683. if (length >= buflen) {
  684. ldm_error ("Truncating string %d -> %d.", length, buflen);
  685. length = buflen - 1;
  686. }
  687. memcpy (buffer, block + 1, length);
  688. buffer[length] = 0;
  689. return length;
  690. }
  691. /**
  692. * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
  693. * @buffer: Block of data being worked on
  694. * @buflen: Size of the block of data
  695. * @vb: In-memory vblk in which to return information
  696. *
  697. * Read a raw VBLK Component object (version 3) into a vblk structure.
  698. *
  699. * Return: 'true' @vb contains a Component VBLK
  700. * 'false' @vb contents are not defined
  701. */
  702. static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
  703. {
  704. int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
  705. struct vblk_comp *comp;
  706. BUG_ON (!buffer || !vb);
  707. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  708. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  709. r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
  710. r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
  711. r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
  712. if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
  713. r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
  714. r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
  715. len = r_cols;
  716. } else {
  717. r_stripe = 0;
  718. r_cols = 0;
  719. len = r_parent;
  720. }
  721. if (len < 0)
  722. return false;
  723. len += VBLK_SIZE_CMP3;
  724. if (len != get_unaligned_be32(buffer + 0x14))
  725. return false;
  726. comp = &vb->vblk.comp;
  727. ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
  728. sizeof (comp->state));
  729. comp->type = buffer[0x18 + r_vstate];
  730. comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
  731. comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
  732. comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
  733. return true;
  734. }
  735. /**
  736. * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
  737. * @buffer: Block of data being worked on
  738. * @buflen: Size of the block of data
  739. * @vb: In-memory vblk in which to return information
  740. *
  741. * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
  742. *
  743. * Return: 'true' @vb contains a Disk Group VBLK
  744. * 'false' @vb contents are not defined
  745. */
  746. static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
  747. {
  748. int r_objid, r_name, r_diskid, r_id1, r_id2, len;
  749. struct vblk_dgrp *dgrp;
  750. BUG_ON (!buffer || !vb);
  751. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  752. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  753. r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
  754. if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
  755. r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
  756. r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
  757. len = r_id2;
  758. } else {
  759. r_id1 = 0;
  760. r_id2 = 0;
  761. len = r_diskid;
  762. }
  763. if (len < 0)
  764. return false;
  765. len += VBLK_SIZE_DGR3;
  766. if (len != get_unaligned_be32(buffer + 0x14))
  767. return false;
  768. dgrp = &vb->vblk.dgrp;
  769. ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
  770. sizeof (dgrp->disk_id));
  771. return true;
  772. }
  773. /**
  774. * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
  775. * @buffer: Block of data being worked on
  776. * @buflen: Size of the block of data
  777. * @vb: In-memory vblk in which to return information
  778. *
  779. * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
  780. *
  781. * Return: 'true' @vb contains a Disk Group VBLK
  782. * 'false' @vb contents are not defined
  783. */
  784. static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
  785. {
  786. char buf[64];
  787. int r_objid, r_name, r_id1, r_id2, len;
  788. struct vblk_dgrp *dgrp;
  789. BUG_ON (!buffer || !vb);
  790. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  791. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  792. if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
  793. r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
  794. r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
  795. len = r_id2;
  796. } else {
  797. r_id1 = 0;
  798. r_id2 = 0;
  799. len = r_name;
  800. }
  801. if (len < 0)
  802. return false;
  803. len += VBLK_SIZE_DGR4;
  804. if (len != get_unaligned_be32(buffer + 0x14))
  805. return false;
  806. dgrp = &vb->vblk.dgrp;
  807. ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
  808. return true;
  809. }
  810. /**
  811. * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
  812. * @buffer: Block of data being worked on
  813. * @buflen: Size of the block of data
  814. * @vb: In-memory vblk in which to return information
  815. *
  816. * Read a raw VBLK Disk object (version 3) into a vblk structure.
  817. *
  818. * Return: 'true' @vb contains a Disk VBLK
  819. * 'false' @vb contents are not defined
  820. */
  821. static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
  822. {
  823. int r_objid, r_name, r_diskid, r_altname, len;
  824. struct vblk_disk *disk;
  825. BUG_ON (!buffer || !vb);
  826. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  827. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  828. r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
  829. r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
  830. len = r_altname;
  831. if (len < 0)
  832. return false;
  833. len += VBLK_SIZE_DSK3;
  834. if (len != get_unaligned_be32(buffer + 0x14))
  835. return false;
  836. disk = &vb->vblk.disk;
  837. ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
  838. sizeof (disk->alt_name));
  839. if (!ldm_parse_guid (buffer + 0x19 + r_name, disk->disk_id))
  840. return false;
  841. return true;
  842. }
  843. /**
  844. * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
  845. * @buffer: Block of data being worked on
  846. * @buflen: Size of the block of data
  847. * @vb: In-memory vblk in which to return information
  848. *
  849. * Read a raw VBLK Disk object (version 4) into a vblk structure.
  850. *
  851. * Return: 'true' @vb contains a Disk VBLK
  852. * 'false' @vb contents are not defined
  853. */
  854. static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
  855. {
  856. int r_objid, r_name, len;
  857. struct vblk_disk *disk;
  858. BUG_ON (!buffer || !vb);
  859. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  860. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  861. len = r_name;
  862. if (len < 0)
  863. return false;
  864. len += VBLK_SIZE_DSK4;
  865. if (len != get_unaligned_be32(buffer + 0x14))
  866. return false;
  867. disk = &vb->vblk.disk;
  868. memcpy (disk->disk_id, buffer + 0x18 + r_name, GUID_SIZE);
  869. return true;
  870. }
  871. /**
  872. * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
  873. * @buffer: Block of data being worked on
  874. * @buflen: Size of the block of data
  875. * @vb: In-memory vblk in which to return information
  876. *
  877. * Read a raw VBLK Partition object (version 3) into a vblk structure.
  878. *
  879. * Return: 'true' @vb contains a Partition VBLK
  880. * 'false' @vb contents are not defined
  881. */
  882. static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
  883. {
  884. int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
  885. struct vblk_part *part;
  886. BUG_ON(!buffer || !vb);
  887. r_objid = ldm_relative(buffer, buflen, 0x18, 0);
  888. if (r_objid < 0) {
  889. ldm_error("r_objid %d < 0", r_objid);
  890. return false;
  891. }
  892. r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
  893. if (r_name < 0) {
  894. ldm_error("r_name %d < 0", r_name);
  895. return false;
  896. }
  897. r_size = ldm_relative(buffer, buflen, 0x34, r_name);
  898. if (r_size < 0) {
  899. ldm_error("r_size %d < 0", r_size);
  900. return false;
  901. }
  902. r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
  903. if (r_parent < 0) {
  904. ldm_error("r_parent %d < 0", r_parent);
  905. return false;
  906. }
  907. r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
  908. if (r_diskid < 0) {
  909. ldm_error("r_diskid %d < 0", r_diskid);
  910. return false;
  911. }
  912. if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
  913. r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
  914. if (r_index < 0) {
  915. ldm_error("r_index %d < 0", r_index);
  916. return false;
  917. }
  918. len = r_index;
  919. } else {
  920. r_index = 0;
  921. len = r_diskid;
  922. }
  923. if (len < 0) {
  924. ldm_error("len %d < 0", len);
  925. return false;
  926. }
  927. len += VBLK_SIZE_PRT3;
  928. if (len > get_unaligned_be32(buffer + 0x14)) {
  929. ldm_error("len %d > BE32(buffer + 0x14) %d", len,
  930. get_unaligned_be32(buffer + 0x14));
  931. return false;
  932. }
  933. part = &vb->vblk.part;
  934. part->start = get_unaligned_be64(buffer + 0x24 + r_name);
  935. part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
  936. part->size = ldm_get_vnum(buffer + 0x34 + r_name);
  937. part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
  938. part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
  939. if (vb->flags & VBLK_FLAG_PART_INDEX)
  940. part->partnum = buffer[0x35 + r_diskid];
  941. else
  942. part->partnum = 0;
  943. return true;
  944. }
  945. /**
  946. * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
  947. * @buffer: Block of data being worked on
  948. * @buflen: Size of the block of data
  949. * @vb: In-memory vblk in which to return information
  950. *
  951. * Read a raw VBLK Volume object (version 5) into a vblk structure.
  952. *
  953. * Return: 'true' @vb contains a Volume VBLK
  954. * 'false' @vb contents are not defined
  955. */
  956. static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
  957. {
  958. int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
  959. int r_id1, r_id2, r_size2, r_drive, len;
  960. struct vblk_volu *volu;
  961. BUG_ON(!buffer || !vb);
  962. r_objid = ldm_relative(buffer, buflen, 0x18, 0);
  963. if (r_objid < 0) {
  964. ldm_error("r_objid %d < 0", r_objid);
  965. return false;
  966. }
  967. r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
  968. if (r_name < 0) {
  969. ldm_error("r_name %d < 0", r_name);
  970. return false;
  971. }
  972. r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
  973. if (r_vtype < 0) {
  974. ldm_error("r_vtype %d < 0", r_vtype);
  975. return false;
  976. }
  977. r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
  978. if (r_disable_drive_letter < 0) {
  979. ldm_error("r_disable_drive_letter %d < 0",
  980. r_disable_drive_letter);
  981. return false;
  982. }
  983. r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
  984. if (r_child < 0) {
  985. ldm_error("r_child %d < 0", r_child);
  986. return false;
  987. }
  988. r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
  989. if (r_size < 0) {
  990. ldm_error("r_size %d < 0", r_size);
  991. return false;
  992. }
  993. if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
  994. r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
  995. if (r_id1 < 0) {
  996. ldm_error("r_id1 %d < 0", r_id1);
  997. return false;
  998. }
  999. } else
  1000. r_id1 = r_size;
  1001. if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
  1002. r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
  1003. if (r_id2 < 0) {
  1004. ldm_error("r_id2 %d < 0", r_id2);
  1005. return false;
  1006. }
  1007. } else
  1008. r_id2 = r_id1;
  1009. if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
  1010. r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
  1011. if (r_size2 < 0) {
  1012. ldm_error("r_size2 %d < 0", r_size2);
  1013. return false;
  1014. }
  1015. } else
  1016. r_size2 = r_id2;
  1017. if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
  1018. r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
  1019. if (r_drive < 0) {
  1020. ldm_error("r_drive %d < 0", r_drive);
  1021. return false;
  1022. }
  1023. } else
  1024. r_drive = r_size2;
  1025. len = r_drive;
  1026. if (len < 0) {
  1027. ldm_error("len %d < 0", len);
  1028. return false;
  1029. }
  1030. len += VBLK_SIZE_VOL5;
  1031. if (len > get_unaligned_be32(buffer + 0x14)) {
  1032. ldm_error("len %d > BE32(buffer + 0x14) %d", len,
  1033. get_unaligned_be32(buffer + 0x14));
  1034. return false;
  1035. }
  1036. volu = &vb->vblk.volu;
  1037. ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
  1038. sizeof(volu->volume_type));
  1039. memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
  1040. sizeof(volu->volume_state));
  1041. volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
  1042. volu->partition_type = buffer[0x41 + r_size];
  1043. memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
  1044. if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
  1045. ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
  1046. sizeof(volu->drive_hint));
  1047. }
  1048. return true;
  1049. }
  1050. /**
  1051. * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
  1052. * @buf: Block of data being worked on
  1053. * @len: Size of the block of data
  1054. * @vb: In-memory vblk in which to return information
  1055. *
  1056. * Read a raw VBLK object into a vblk structure. This function just reads the
  1057. * information common to all VBLK types, then delegates the rest of the work to
  1058. * helper functions: ldm_parse_*.
  1059. *
  1060. * Return: 'true' @vb contains a VBLK
  1061. * 'false' @vb contents are not defined
  1062. */
  1063. static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
  1064. {
  1065. bool result = false;
  1066. int r_objid;
  1067. BUG_ON (!buf || !vb);
  1068. r_objid = ldm_relative (buf, len, 0x18, 0);
  1069. if (r_objid < 0) {
  1070. ldm_error ("VBLK header is corrupt.");
  1071. return false;
  1072. }
  1073. vb->flags = buf[0x12];
  1074. vb->type = buf[0x13];
  1075. vb->obj_id = ldm_get_vnum (buf + 0x18);
  1076. ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
  1077. switch (vb->type) {
  1078. case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
  1079. case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
  1080. case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
  1081. case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
  1082. case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
  1083. case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
  1084. case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
  1085. }
  1086. if (result)
  1087. ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
  1088. (unsigned long long) vb->obj_id, vb->type);
  1089. else
  1090. ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
  1091. (unsigned long long) vb->obj_id, vb->type);
  1092. return result;
  1093. }
  1094. /**
  1095. * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
  1096. * @data: Raw VBLK to add to the database
  1097. * @len: Size of the raw VBLK
  1098. * @ldb: Cache of the database structures
  1099. *
  1100. * The VBLKs are sorted into categories. Partitions are also sorted by offset.
  1101. *
  1102. * N.B. This function does not check the validity of the VBLKs.
  1103. *
  1104. * Return: 'true' The VBLK was added
  1105. * 'false' An error occurred
  1106. */
  1107. static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
  1108. {
  1109. struct vblk *vb;
  1110. struct list_head *item;
  1111. BUG_ON (!data || !ldb);
  1112. vb = kmalloc (sizeof (*vb), GFP_KERNEL);
  1113. if (!vb) {
  1114. ldm_crit ("Out of memory.");
  1115. return false;
  1116. }
  1117. if (!ldm_parse_vblk (data, len, vb)) {
  1118. kfree(vb);
  1119. return false; /* Already logged */
  1120. }
  1121. /* Put vblk into the correct list. */
  1122. switch (vb->type) {
  1123. case VBLK_DGR3:
  1124. case VBLK_DGR4:
  1125. list_add (&vb->list, &ldb->v_dgrp);
  1126. break;
  1127. case VBLK_DSK3:
  1128. case VBLK_DSK4:
  1129. list_add (&vb->list, &ldb->v_disk);
  1130. break;
  1131. case VBLK_VOL5:
  1132. list_add (&vb->list, &ldb->v_volu);
  1133. break;
  1134. case VBLK_CMP3:
  1135. list_add (&vb->list, &ldb->v_comp);
  1136. break;
  1137. case VBLK_PRT3:
  1138. /* Sort by the partition's start sector. */
  1139. list_for_each (item, &ldb->v_part) {
  1140. struct vblk *v = list_entry (item, struct vblk, list);
  1141. if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
  1142. (v->vblk.part.start > vb->vblk.part.start)) {
  1143. list_add_tail (&vb->list, &v->list);
  1144. return true;
  1145. }
  1146. }
  1147. list_add_tail (&vb->list, &ldb->v_part);
  1148. break;
  1149. }
  1150. return true;
  1151. }
  1152. /**
  1153. * ldm_frag_add - Add a VBLK fragment to a list
  1154. * @data: Raw fragment to be added to the list
  1155. * @size: Size of the raw fragment
  1156. * @frags: Linked list of VBLK fragments
  1157. *
  1158. * Fragmented VBLKs may not be consecutive in the database, so they are placed
  1159. * in a list so they can be pieced together later.
  1160. *
  1161. * Return: 'true' Success, the VBLK was added to the list
  1162. * 'false' Error, a problem occurred
  1163. */
  1164. static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
  1165. {
  1166. struct frag *f;
  1167. struct list_head *item;
  1168. int rec, num, group;
  1169. BUG_ON (!data || !frags);
  1170. if (size < 2 * VBLK_SIZE_HEAD) {
  1171. ldm_error("Value of size is to small.");
  1172. return false;
  1173. }
  1174. group = get_unaligned_be32(data + 0x08);
  1175. rec = get_unaligned_be16(data + 0x0C);
  1176. num = get_unaligned_be16(data + 0x0E);
  1177. if ((num < 1) || (num > 4)) {
  1178. ldm_error ("A VBLK claims to have %d parts.", num);
  1179. return false;
  1180. }
  1181. if (rec >= num) {
  1182. ldm_error("REC value (%d) exceeds NUM value (%d)", rec, num);
  1183. return false;
  1184. }
  1185. list_for_each (item, frags) {
  1186. f = list_entry (item, struct frag, list);
  1187. if (f->group == group)
  1188. goto found;
  1189. }
  1190. f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
  1191. if (!f) {
  1192. ldm_crit ("Out of memory.");
  1193. return false;
  1194. }
  1195. f->group = group;
  1196. f->num = num;
  1197. f->rec = rec;
  1198. f->map = 0xFF << num;
  1199. list_add_tail (&f->list, frags);
  1200. found:
  1201. if (rec >= f->num) {
  1202. ldm_error("REC value (%d) exceeds NUM value (%d)", rec, f->num);
  1203. return false;
  1204. }
  1205. if (f->map & (1 << rec)) {
  1206. ldm_error ("Duplicate VBLK, part %d.", rec);
  1207. f->map &= 0x7F; /* Mark the group as broken */
  1208. return false;
  1209. }
  1210. f->map |= (1 << rec);
  1211. data += VBLK_SIZE_HEAD;
  1212. size -= VBLK_SIZE_HEAD;
  1213. memcpy (f->data+rec*(size-VBLK_SIZE_HEAD)+VBLK_SIZE_HEAD, data, size);
  1214. return true;
  1215. }
  1216. /**
  1217. * ldm_frag_free - Free a linked list of VBLK fragments
  1218. * @list: Linked list of fragments
  1219. *
  1220. * Free a linked list of VBLK fragments
  1221. *
  1222. * Return: none
  1223. */
  1224. static void ldm_frag_free (struct list_head *list)
  1225. {
  1226. struct list_head *item, *tmp;
  1227. BUG_ON (!list);
  1228. list_for_each_safe (item, tmp, list)
  1229. kfree (list_entry (item, struct frag, list));
  1230. }
  1231. /**
  1232. * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
  1233. * @frags: Linked list of VBLK fragments
  1234. * @ldb: Cache of the database structures
  1235. *
  1236. * Now that all the fragmented VBLKs have been collected, they must be added to
  1237. * the database for later use.
  1238. *
  1239. * Return: 'true' All the fragments we added successfully
  1240. * 'false' One or more of the fragments we invalid
  1241. */
  1242. static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
  1243. {
  1244. struct frag *f;
  1245. struct list_head *item;
  1246. BUG_ON (!frags || !ldb);
  1247. list_for_each (item, frags) {
  1248. f = list_entry (item, struct frag, list);
  1249. if (f->map != 0xFF) {
  1250. ldm_error ("VBLK group %d is incomplete (0x%02x).",
  1251. f->group, f->map);
  1252. return false;
  1253. }
  1254. if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
  1255. return false; /* Already logged */
  1256. }
  1257. return true;
  1258. }
  1259. /**
  1260. * ldm_get_vblks - Read the on-disk database of VBLKs into memory
  1261. * @state: Partition check state including device holding the LDM Database
  1262. * @base: Offset, into @state->bdev, of the database
  1263. * @ldb: Cache of the database structures
  1264. *
  1265. * To use the information from the VBLKs, they need to be read from the disk,
  1266. * unpacked and validated. We cache them in @ldb according to their type.
  1267. *
  1268. * Return: 'true' All the VBLKs were read successfully
  1269. * 'false' An error occurred
  1270. */
  1271. static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
  1272. struct ldmdb *ldb)
  1273. {
  1274. int size, perbuf, skip, finish, s, v, recs;
  1275. u8 *data = NULL;
  1276. Sector sect;
  1277. bool result = false;
  1278. LIST_HEAD (frags);
  1279. BUG_ON(!state || !ldb);
  1280. size = ldb->vm.vblk_size;
  1281. perbuf = 512 / size;
  1282. skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
  1283. finish = (size * ldb->vm.last_vblk_seq) >> 9;
  1284. for (s = skip; s < finish; s++) { /* For each sector */
  1285. data = read_part_sector(state, base + OFF_VMDB + s, &sect);
  1286. if (!data) {
  1287. ldm_crit ("Disk read failed.");
  1288. goto out;
  1289. }
  1290. for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
  1291. if (MAGIC_VBLK != get_unaligned_be32(data)) {
  1292. ldm_error ("Expected to find a VBLK.");
  1293. goto out;
  1294. }
  1295. recs = get_unaligned_be16(data + 0x0E); /* Number of records */
  1296. if (recs == 1) {
  1297. if (!ldm_ldmdb_add (data, size, ldb))
  1298. goto out; /* Already logged */
  1299. } else if (recs > 1) {
  1300. if (!ldm_frag_add (data, size, &frags))
  1301. goto out; /* Already logged */
  1302. }
  1303. /* else Record is not in use, ignore it. */
  1304. }
  1305. put_dev_sector (sect);
  1306. data = NULL;
  1307. }
  1308. result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
  1309. out:
  1310. if (data)
  1311. put_dev_sector (sect);
  1312. ldm_frag_free (&frags);
  1313. return result;
  1314. }
  1315. /**
  1316. * ldm_free_vblks - Free a linked list of vblk's
  1317. * @lh: Head of a linked list of struct vblk
  1318. *
  1319. * Free a list of vblk's and free the memory used to maintain the list.
  1320. *
  1321. * Return: none
  1322. */
  1323. static void ldm_free_vblks (struct list_head *lh)
  1324. {
  1325. struct list_head *item, *tmp;
  1326. BUG_ON (!lh);
  1327. list_for_each_safe (item, tmp, lh)
  1328. kfree (list_entry (item, struct vblk, list));
  1329. }
  1330. /**
  1331. * ldm_partition - Find out whether a device is a dynamic disk and handle it
  1332. * @state: Partition check state including device holding the LDM Database
  1333. *
  1334. * This determines whether the device @bdev is a dynamic disk and if so creates
  1335. * the partitions necessary in the gendisk structure pointed to by @hd.
  1336. *
  1337. * We create a dummy device 1, which contains the LDM database, and then create
  1338. * each partition described by the LDM database in sequence as devices 2+. For
  1339. * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
  1340. * and so on: the actual data containing partitions.
  1341. *
  1342. * Return: 1 Success, @state->bdev is a dynamic disk and we handled it
  1343. * 0 Success, @state->bdev is not a dynamic disk
  1344. * -1 An error occurred before enough information had been read
  1345. * Or @state->bdev is a dynamic disk, but it may be corrupted
  1346. */
  1347. int ldm_partition(struct parsed_partitions *state)
  1348. {
  1349. struct ldmdb *ldb;
  1350. unsigned long base;
  1351. int result = -1;
  1352. BUG_ON(!state);
  1353. /* Look for signs of a Dynamic Disk */
  1354. if (!ldm_validate_partition_table(state))
  1355. return 0;
  1356. ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
  1357. if (!ldb) {
  1358. ldm_crit ("Out of memory.");
  1359. goto out;
  1360. }
  1361. /* Parse and check privheads. */
  1362. if (!ldm_validate_privheads(state, &ldb->ph))
  1363. goto out; /* Already logged */
  1364. /* All further references are relative to base (database start). */
  1365. base = ldb->ph.config_start;
  1366. /* Parse and check tocs and vmdb. */
  1367. if (!ldm_validate_tocblocks(state, base, ldb) ||
  1368. !ldm_validate_vmdb(state, base, ldb))
  1369. goto out; /* Already logged */
  1370. /* Initialize vblk lists in ldmdb struct */
  1371. INIT_LIST_HEAD (&ldb->v_dgrp);
  1372. INIT_LIST_HEAD (&ldb->v_disk);
  1373. INIT_LIST_HEAD (&ldb->v_volu);
  1374. INIT_LIST_HEAD (&ldb->v_comp);
  1375. INIT_LIST_HEAD (&ldb->v_part);
  1376. if (!ldm_get_vblks(state, base, ldb)) {
  1377. ldm_crit ("Failed to read the VBLKs from the database.");
  1378. goto cleanup;
  1379. }
  1380. /* Finally, create the data partition devices. */
  1381. if (ldm_create_data_partitions(state, ldb)) {
  1382. ldm_debug ("Parsed LDM database successfully.");
  1383. result = 1;
  1384. }
  1385. /* else Already logged */
  1386. cleanup:
  1387. ldm_free_vblks (&ldb->v_dgrp);
  1388. ldm_free_vblks (&ldb->v_disk);
  1389. ldm_free_vblks (&ldb->v_volu);
  1390. ldm_free_vblks (&ldb->v_comp);
  1391. ldm_free_vblks (&ldb->v_part);
  1392. out:
  1393. kfree (ldb);
  1394. return result;
  1395. }