ldm.c 44 KB

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