setup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. setup.c
  9. Abstract:
  10. This module implements the main setup executable.
  11. Author:
  12. Evan Green 10-Apr-2014
  13. Environment:
  14. User
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include "setup.h"
  31. #include "sconf.h"
  32. //
  33. // ---------------------------------------------------------------- Definitions
  34. //
  35. #define SETUP_VERSION_MAJOR 1
  36. #define SETUP_VERSION_MINOR 0
  37. #define SETUP_USAGE \
  38. "usage: msetup [-v] [-d|-p|-f destination]\n" \
  39. "Setup installs Minoca OS to a new destination. Options are:\n" \
  40. " -a, --page-file=size -- Specifies the size in megabytes of the page \n" \
  41. " file to create. Specify 0 to skip page file creation. If not \n" \
  42. " supplied, a default value of 1.5x the amount of memory on the \n" \
  43. " system will be used.\n" \
  44. " -b, --boot=destination -- Specifies the boot partition to install to.\n"\
  45. " -B, --boot-debug -- Enable boot debugging on the target installation.\n"\
  46. " -D, --debug -- Enable debugging on the target installation.\n" \
  47. " -d, --disk=destination -- Specifies the install destination as a " \
  48. "disk.\n" \
  49. " -f, --directory=destination -- Specifies the install destination as \n" \
  50. " a directory.\n" \
  51. " -G, --disk-size=size -- Specifies the disk size if the install \n" \
  52. " destination is a disk. Suffixes M, G, and T are permitted.\n" \
  53. " -i, --input=image -- Specifies the location of the installation \n" \
  54. " image. The default is to open install.img in the current " \
  55. "directory.\n" \
  56. " If the specified image is a file, it will be opened as an image. \n"\
  57. " The input can also be a directory.\n" \
  58. " -l, --platform=name -- Specifies the platform type.\n" \
  59. " -p, --partition=destination -- Specifies the install destination as \n" \
  60. " a partition.\n" \
  61. " -q, --quiet -- Print nothing but errors.\n" \
  62. " -r, --reboot -- Reboot after installation is complete.\n" \
  63. " -s, --script=file -- Load a script file.\n" \
  64. " -v, --verbose -- Print files being copied.\n" \
  65. " -x, --extra-partition=size -- Add an extra partition. Supply -1 to \n" \
  66. " split the remaining space with the system partition. This can be \n"\
  67. " specified multiple times.\n" \
  68. " --help -- Show this help text and exit.\n" \
  69. " --version -- Print the application version information and exit.\n\n" \
  70. "The destination parameter can take the form of a device ID starting \n" \
  71. "with 0x or a path.\n" \
  72. "Example: 'msetup -v -p 0x26' Installs on a partition with device ID " \
  73. "0x26.\n"
  74. #define SETUP_OPTIONS_STRING "Aa:b:BDd:G:hi:l:p:f:qrs:vx:V"
  75. #define SETUP_ADD_PARTITION_SCRIPT_FORMAT \
  76. "Partitions.append({" \
  77. "\"Index\": %d," \
  78. "\"Size\": %lld," \
  79. "\"PartitionType\": PARTITION_TYPE_MINOCA," \
  80. "\"MbrType\": PARTITION_ID_MINOCA," \
  81. "\"Attributes\": 0," \
  82. "\"Alignment\": 4 * KILOBYTE});"
  83. //
  84. // ------------------------------------------------------ Data Type Definitions
  85. //
  86. //
  87. // ----------------------------------------------- Internal Function Prototypes
  88. //
  89. INT
  90. SetupAddExtraPartition (
  91. PSETUP_CONTEXT Context,
  92. ULONG Index,
  93. LONGLONG Size
  94. );
  95. INT
  96. SetupDetermineAutodeployDestination (
  97. PSETUP_CONTEXT Context
  98. );
  99. //
  100. // -------------------------------------------------------------------- Globals
  101. //
  102. struct option SetupLongOptions[] = {
  103. {"autodeploy", no_argument, 0, 'A'},
  104. {"page-file", required_argument, 0, 'a'},
  105. {"boot-debug", no_argument, 0, 'B'},
  106. {"boot", required_argument, 0, 'b'},
  107. {"debug", no_argument, 0, 'D'},
  108. {"disk", required_argument, 0, 'd'},
  109. {"directory", required_argument, 0, 'f'},
  110. {"disk-size", required_argument, 0, 'G'},
  111. {"help", no_argument, 0, 'h'},
  112. {"input", required_argument, 0, 'i'},
  113. {"platform", required_argument, 0, 'l'},
  114. {"partition", required_argument, 0, 'p'},
  115. {"quiet", no_argument, 0, 'q'},
  116. {"reboot", no_argument, 0, 'r'},
  117. {"script", required_argument, 0, 's'},
  118. {"version", no_argument, 0, 'V'},
  119. {"verbose", no_argument, 0, 'v'},
  120. {NULL, 0, 0, 0},
  121. };
  122. //
  123. // ------------------------------------------------------------------ Functions
  124. //
  125. int
  126. main (
  127. int ArgumentCount,
  128. char **Arguments
  129. )
  130. /*++
  131. Routine Description:
  132. This routine implements the setup user mode program.
  133. Arguments:
  134. ArgumentCount - Supplies the number of elements in the arguments array.
  135. Arguments - Supplies an array of strings. The array count is bounded by the
  136. previous parameter, and the strings are null-terminated.
  137. Return Value:
  138. 0 on success.
  139. Non-zero on failure.
  140. --*/
  141. {
  142. PSTR AfterScan;
  143. PVOID BootVolume;
  144. CK_CONFIGURATION ChalkConfiguration;
  145. SETUP_CONTEXT Context;
  146. ULONG DeviceCount;
  147. ULONG DeviceIndex;
  148. PSETUP_PARTITION_DESCRIPTION Devices;
  149. ULONG ExtraPartitionIndex;
  150. LONGLONG ExtraPartitionSize;
  151. PSETUP_DESTINATION HostFileSystemPath;
  152. PSTR HostPathString;
  153. PSTR InstallImagePath;
  154. INT Option;
  155. PSTR PlatformName;
  156. BOOL PrintHeader;
  157. BOOL QuietlyQuit;
  158. PSETUP_DESTINATION SourcePath;
  159. struct stat Stat;
  160. INT Status;
  161. BootVolume = NULL;
  162. DeviceCount = 0;
  163. Devices = NULL;
  164. ExtraPartitionIndex = 100;
  165. HostPathString = "";
  166. InstallImagePath = SETUP_DEFAULT_IMAGE_NAME;
  167. PlatformName = NULL;
  168. QuietlyQuit = FALSE;
  169. SourcePath = NULL;
  170. srand(time(NULL) ^ getpid());
  171. memset(&Context, 0, sizeof(SETUP_CONTEXT));
  172. Context.PageFileSize = -1ULL;
  173. CkInitializeConfiguration(&ChalkConfiguration);
  174. Context.ChalkVm = CkCreateVm(&ChalkConfiguration);
  175. if (Context.ChalkVm == NULL) {
  176. Status = ENOMEM;
  177. goto mainEnd;
  178. }
  179. //
  180. // Process the control arguments.
  181. //
  182. while (TRUE) {
  183. Option = getopt_long(ArgumentCount,
  184. Arguments,
  185. SETUP_OPTIONS_STRING,
  186. SetupLongOptions,
  187. NULL);
  188. if (Option == -1) {
  189. break;
  190. }
  191. if ((Option == '?') || (Option == ':')) {
  192. Status = -1;
  193. goto mainEnd;
  194. }
  195. switch (Option) {
  196. case 'A':
  197. Context.Flags |= SETUP_FLAG_AUTO_DEPLOY;
  198. break;
  199. case 'a':
  200. Context.PageFileSize = strtoul(optarg, &AfterScan, 0);
  201. if (AfterScan == optarg) {
  202. fprintf(stderr,
  203. "Error: Invalid page file megabytes: '%s'.\n",
  204. optarg);
  205. Status = EINVAL;
  206. goto mainEnd;
  207. }
  208. break;
  209. case 'b':
  210. Context.BootPartitionPath = SetupParseDestination(
  211. SetupDestinationPartition,
  212. optarg);
  213. break;
  214. case 'B':
  215. Context.Flags |= SETUP_FLAG_INSTALL_BOOT_DEBUG;
  216. break;
  217. case 'D':
  218. Context.Flags |= SETUP_FLAG_INSTALL_DEBUG;
  219. break;
  220. case 'd':
  221. Context.DiskPath = SetupParseDestination(SetupDestinationDisk,
  222. optarg);
  223. break;
  224. case 'f':
  225. Context.DirectoryPath = SetupParseDestination(
  226. SetupDestinationDirectory,
  227. optarg);
  228. break;
  229. case 'G':
  230. Context.DiskSize = strtoull(optarg, &AfterScan, 0);
  231. if (AfterScan == optarg) {
  232. fprintf(stderr, "Error: Invalid disk size '%s'.\n", optarg);
  233. Status = EINVAL;
  234. goto mainEnd;
  235. }
  236. switch (toupper(*AfterScan)) {
  237. case '\0':
  238. break;
  239. case 'K':
  240. Context.DiskSize *= _1KB;
  241. break;
  242. case 'M':
  243. Context.DiskSize *= _1MB;
  244. break;
  245. case 'G':
  246. Context.DiskSize *= _1GB;
  247. break;
  248. case 'T':
  249. Context.DiskSize *= _1TB;
  250. break;
  251. default:
  252. fprintf(stderr, "Error: Invalid suffix %s.\n", AfterScan);
  253. Status = EINVAL;
  254. goto mainEnd;
  255. }
  256. break;
  257. case 'i':
  258. InstallImagePath = optarg;
  259. break;
  260. case 'p':
  261. Context.PartitionPath = SetupParseDestination(
  262. SetupDestinationPartition,
  263. optarg);
  264. break;
  265. case 'l':
  266. if (SetupParsePlatformString(&Context, optarg) == FALSE) {
  267. fprintf(stderr, "Error: Invalid platform '%s'.\n", optarg);
  268. SetupPrintPlatformList();
  269. Status = EINVAL;
  270. goto mainEnd;
  271. }
  272. break;
  273. //
  274. // User scripts are handled later.
  275. //
  276. case 's':
  277. break;
  278. case 'q':
  279. Context.Flags |= SETUP_FLAG_QUIET;
  280. break;
  281. case 'r':
  282. Context.Flags |= SETUP_FLAG_REBOOT;
  283. break;
  284. case 'v':
  285. Context.Flags |= SETUP_FLAG_VERBOSE;
  286. break;
  287. //
  288. // Extra partitions are handled later.
  289. //
  290. case 'x':
  291. break;
  292. case 'V':
  293. printf("Minoca setup version %d.%d\n"
  294. "Copyright (c) 2014-2016 Minoca Corp. "
  295. "All Rights Reserved.\n\n",
  296. SETUP_VERSION_MAJOR,
  297. SETUP_VERSION_MINOR);
  298. return 1;
  299. case 'h':
  300. printf(SETUP_USAGE);
  301. SetupPrintPlatformList();
  302. return 1;
  303. default:
  304. assert(FALSE);
  305. Status = 1;
  306. goto mainEnd;
  307. }
  308. }
  309. //
  310. // If autodeploy was specified, figure out what partition to install to.
  311. //
  312. if ((Context.Flags & SETUP_FLAG_AUTO_DEPLOY) != 0) {
  313. Status = SetupDetermineAutodeployDestination(&Context);
  314. if (Status != 0) {
  315. fprintf(stderr,
  316. "Setup failed to determine autodeploy destination.\n");
  317. goto mainEnd;
  318. }
  319. //
  320. // If no destination was specified, list the possible destinations.
  321. //
  322. } else if ((Context.DiskPath == NULL) && (Context.PartitionPath == NULL) &&
  323. (Context.DirectoryPath == NULL)) {
  324. Status = SetupOsGetPlatformName(&PlatformName, NULL);
  325. if (Status != 0) {
  326. printf("Unable to detect platform name.\n");
  327. } else {
  328. printf("Platform: %s\n", PlatformName);
  329. }
  330. printf("No destination was specified. Please select one from the "
  331. "following list.\n");
  332. //
  333. // Enumerate all eligible setup devices.
  334. //
  335. Status = SetupOsEnumerateDevices(&Devices, &DeviceCount);
  336. if (Status != 0) {
  337. fprintf(stderr, "Error: Failed to enumerate devices.\n");
  338. goto mainEnd;
  339. }
  340. //
  341. // Print a description of the eligible devices.
  342. //
  343. if (DeviceCount == 0) {
  344. printf("Setup found no devices to install to.\n");
  345. } else {
  346. if (DeviceCount == 1) {
  347. printf("Setup found 1 device.\n");
  348. } else {
  349. printf("Setup found %d devices:\n", DeviceCount);
  350. }
  351. PrintHeader = TRUE;
  352. for (DeviceIndex = 0; DeviceIndex < DeviceCount; DeviceIndex += 1) {
  353. SetupPrintDeviceDescription(&(Devices[DeviceIndex]),
  354. PrintHeader);
  355. PrintHeader = FALSE;
  356. }
  357. printf("\n");
  358. }
  359. QuietlyQuit = TRUE;
  360. Status = -1;
  361. goto mainEnd;
  362. }
  363. //
  364. // If the install source is a directory, then open that directory as the
  365. // host file system path.
  366. //
  367. Status = stat(InstallImagePath, &Stat);
  368. if (Status != 0) {
  369. Status = errno;
  370. fprintf(stderr,
  371. "Error: Cannot open input %s: %s\n",
  372. InstallImagePath,
  373. strerror(Status));
  374. goto mainEnd;
  375. }
  376. if (S_ISDIR(Stat.st_mode)) {
  377. HostPathString = InstallImagePath;
  378. }
  379. //
  380. // Create a volume handle to the host file system.
  381. //
  382. HostFileSystemPath = SetupCreateDestination(SetupDestinationDirectory,
  383. HostPathString,
  384. 0);
  385. if (HostFileSystemPath == NULL) {
  386. Status = ENOMEM;
  387. goto mainEnd;
  388. }
  389. Context.HostFileSystem = SetupVolumeOpen(&Context,
  390. HostFileSystemPath,
  391. SetupVolumeFormatNever,
  392. FALSE);
  393. SetupDestroyDestination(HostFileSystemPath);
  394. if (Context.HostFileSystem == NULL) {
  395. fprintf(stderr, "Failed to open local file system.\n");
  396. Status = errno;
  397. if (Status == 0) {
  398. Status = -1;
  399. }
  400. goto mainEnd;
  401. }
  402. //
  403. // Open up the source image.
  404. //
  405. if (S_ISDIR(Stat.st_mode)) {
  406. Context.SourceVolume = Context.HostFileSystem;
  407. } else {
  408. SourcePath = SetupCreateDestination(SetupDestinationImage,
  409. InstallImagePath,
  410. 0);
  411. if (SourcePath == NULL) {
  412. Status = ENOMEM;
  413. goto mainEnd;
  414. }
  415. Context.SourceVolume = SetupVolumeOpen(&Context,
  416. SourcePath,
  417. SetupVolumeFormatNever,
  418. FALSE);
  419. if (Context.SourceVolume == NULL) {
  420. fprintf(stderr,
  421. "Setup failed to open the install source: %s.\n",
  422. InstallImagePath);
  423. Status = -1;
  424. goto mainEnd;
  425. }
  426. }
  427. //
  428. // Detect the platform type if it was not yet done.
  429. //
  430. if (Context.PlatformName == NULL) {
  431. Status = SetupDeterminePlatform(&Context);
  432. if (Status != 0) {
  433. fprintf(stderr,
  434. "Error: Could not detect platform type. Specify -l "
  435. "manually.\n");
  436. goto mainEnd;
  437. }
  438. }
  439. //
  440. // Read in and run the configuration script.
  441. //
  442. Status = SetupLoadConfiguration(&Context);
  443. if (Status != 0) {
  444. goto mainEnd;
  445. }
  446. //
  447. // Go through the options again now that the configuration has been loaded
  448. // so it can potentially be tweaked by the user.
  449. //
  450. optind = 1;
  451. while (TRUE) {
  452. Option = getopt_long(ArgumentCount,
  453. Arguments,
  454. SETUP_OPTIONS_STRING,
  455. SetupLongOptions,
  456. NULL);
  457. if (Option == -1) {
  458. break;
  459. }
  460. assert((Option != '?') && (Option != ':'));
  461. switch (Option) {
  462. case 'A':
  463. Status = SetupAddExtraPartition(&Context,
  464. ExtraPartitionIndex,
  465. -1LL);
  466. if (Status != 0) {
  467. fprintf(stderr, "Error: Failed to add extra partition.\n");
  468. goto mainEnd;
  469. }
  470. ExtraPartitionIndex += 1;
  471. break;
  472. case 's':
  473. Status = SetupLoadUserScript(&Context, optarg);
  474. if (Status != 0) {
  475. fprintf(stderr,
  476. "Error: Failed to load script %s: %s.\n",
  477. optarg,
  478. strerror(Status));
  479. goto mainEnd;
  480. }
  481. break;
  482. case 'x':
  483. ExtraPartitionSize = strtoll(optarg, &AfterScan, 0);
  484. if (AfterScan == optarg) {
  485. fprintf(stderr, "Error: Invalid Size %s.\n", optarg);
  486. Status = EINVAL;
  487. goto mainEnd;
  488. }
  489. Status = SetupAddExtraPartition(&Context,
  490. ExtraPartitionIndex,
  491. ExtraPartitionSize);
  492. if (Status != 0) {
  493. fprintf(stderr, "Error: Failed to add extra partition.\n");
  494. goto mainEnd;
  495. }
  496. ExtraPartitionIndex += 1;
  497. break;
  498. //
  499. // Ignore everything else, assume it was handled in the first loop.
  500. //
  501. default:
  502. break;
  503. }
  504. }
  505. Status = SetupReadConfiguration(Context.ChalkVm, &(Context.Configuration));
  506. if (Status != 0) {
  507. perror("Failed to read configuration");
  508. goto mainEnd;
  509. }
  510. //
  511. // Install to a disk.
  512. //
  513. if (Context.DiskPath != NULL) {
  514. Status = SetupInstallToDisk(&Context);
  515. if (Status != 0) {
  516. goto mainEnd;
  517. }
  518. if (((Context.Flags & SETUP_FLAG_REBOOT) == 0) &&
  519. ((Context.Flags & SETUP_FLAG_QUIET) == 0)) {
  520. if ((Context.Flags & SETUP_FLAG_VERBOSE) != 0) {
  521. printf("\n");
  522. }
  523. printf("Remove install media and reboot to continue.\n");
  524. }
  525. //
  526. // There's no disk, open a partition.
  527. //
  528. } else if (Context.PartitionPath != NULL) {
  529. Status = SetupInstallToPartition(&Context, NULL);
  530. if (Status != 0) {
  531. goto mainEnd;
  532. }
  533. } else if (Context.DirectoryPath != NULL) {
  534. Status = SetupInstallToDirectory(&Context);
  535. if (Status != 0) {
  536. goto mainEnd;
  537. }
  538. } else {
  539. fprintf(stderr, "Error: no installation path specified.\n");
  540. Status = EINVAL;
  541. goto mainEnd;
  542. }
  543. //
  544. // If a boot partition was specified, try to update that.
  545. //
  546. BootVolume = NULL;
  547. if (Context.BootPartitionPath != NULL) {
  548. BootVolume = SetupVolumeOpen(&Context,
  549. Context.BootPartitionPath,
  550. SetupVolumeFormatIfIncompatible,
  551. TRUE);
  552. if (BootVolume == NULL) {
  553. fprintf(stderr, "Setup failed to open the boot volume.\n");
  554. SetupPrintDestination(Context.BootPartitionPath);
  555. Status = -1;
  556. goto mainEnd;
  557. }
  558. //
  559. // Update the boot partition for a partition or directory install.
  560. //
  561. } else if ((Context.PartitionPath != NULL) ||
  562. (Context.DirectoryPath != NULL)) {
  563. BootVolume = SetupOsOpenBootVolume(&Context);
  564. if (BootVolume == NULL) {
  565. Status = -1;
  566. fprintf(stderr, "Error: Failed to open boot volume.\n");
  567. goto mainEnd;
  568. }
  569. }
  570. if (BootVolume != NULL) {
  571. Status = SetupUpdateBootVolume(&Context, BootVolume);
  572. if (Status != 0) {
  573. goto mainEnd;
  574. }
  575. }
  576. mainEnd:
  577. if (Context.Configuration != NULL) {
  578. SetupDestroyConfiguration(Context.Configuration);
  579. Context.Configuration = NULL;
  580. }
  581. if (Context.ChalkVm != NULL) {
  582. CkDestroyVm(Context.ChalkVm);
  583. }
  584. if (Context.SourceVolume != NULL) {
  585. if (Context.SourceVolume != Context.HostFileSystem) {
  586. SetupVolumeClose(&Context, Context.SourceVolume);
  587. }
  588. Context.SourceVolume = NULL;
  589. }
  590. if (Context.HostFileSystem != NULL) {
  591. SetupVolumeClose(&Context, Context.HostFileSystem);
  592. Context.HostFileSystem = NULL;
  593. }
  594. if (PlatformName != NULL) {
  595. free(PlatformName);
  596. }
  597. if (SourcePath != NULL) {
  598. SetupDestroyDestination(SourcePath);
  599. }
  600. if (BootVolume != NULL) {
  601. SetupVolumeClose(&Context, BootVolume);
  602. }
  603. if (Context.Disk != NULL) {
  604. SetupClose(Context.Disk);
  605. }
  606. if (Context.DiskPath != NULL) {
  607. SetupDestroyDestination(Context.DiskPath);
  608. }
  609. if (Context.PartitionPath != NULL) {
  610. SetupDestroyDestination(Context.PartitionPath);
  611. }
  612. if (Context.DirectoryPath != NULL) {
  613. SetupDestroyDestination(Context.DirectoryPath);
  614. }
  615. if (Context.BootPartitionPath != NULL) {
  616. SetupDestroyDestination(Context.BootPartitionPath);
  617. }
  618. if (Devices != NULL) {
  619. SetupDestroyDeviceDescriptions(Devices, DeviceCount);
  620. }
  621. if (Status == 0) {
  622. if ((Context.Flags & SETUP_FLAG_REBOOT) != 0) {
  623. if ((Context.Flags & SETUP_FLAG_VERBOSE) != 0) {
  624. printf("Rebooting system...\n");
  625. }
  626. Status = SetupOsReboot();
  627. if (Status != 0) {
  628. fprintf(stderr,
  629. "Reboot failed: %d: %s.\n",
  630. Status,
  631. strerror(Status));
  632. }
  633. }
  634. }
  635. if (Status != 0) {
  636. if ((QuietlyQuit == FALSE) &&
  637. ((Context.Flags & SETUP_FLAG_QUIET) == 0)) {
  638. if (Status > 0) {
  639. printf("Setup exiting with status %d: %s\n",
  640. Status,
  641. strerror(Status));
  642. } else {
  643. printf("Setup failed.\n");
  644. }
  645. }
  646. return 1;
  647. }
  648. if ((Context.Flags & SETUP_FLAG_QUIET) == 0) {
  649. printf("Setup completed successfully.\n");
  650. }
  651. return 0;
  652. }
  653. //
  654. // --------------------------------------------------------- Internal Functions
  655. //
  656. INT
  657. SetupAddExtraPartition (
  658. PSETUP_CONTEXT Context,
  659. ULONG Index,
  660. LONGLONG Size
  661. )
  662. /*++
  663. Routine Description:
  664. This routine adds an extra partition to the configuration.
  665. Arguments:
  666. Context - Supplies a pointer to the applicaton context.
  667. Index - Supplies the partition index to assign.
  668. Size - Supplies the size of the partition to add.
  669. Return Value:
  670. 0 on success.
  671. Non-zero on failure.
  672. --*/
  673. {
  674. CHAR ScriptBuffer[512];
  675. INT ScriptSize;
  676. ScriptSize = snprintf(ScriptBuffer,
  677. sizeof(ScriptBuffer),
  678. SETUP_ADD_PARTITION_SCRIPT_FORMAT,
  679. Index,
  680. Size);
  681. assert((ScriptSize > 0) && (ScriptSize < sizeof(ScriptBuffer)));
  682. return SetupLoadUserExpression(Context, ScriptBuffer);
  683. }
  684. INT
  685. SetupDetermineAutodeployDestination (
  686. PSETUP_CONTEXT Context
  687. )
  688. /*++
  689. Routine Description:
  690. This routine attempts to automatically find the partition to install to.
  691. It does this by finding a partition on the boot disk that is not the boot
  692. partition or the system partition. This option should generally only be
  693. used by test automation, not real users, which is why it is undocumented.
  694. Arguments:
  695. Context - Supplies a pointer to the applicaton context.
  696. Return Value:
  697. 0 on success.
  698. Non-zero on failure.
  699. --*/
  700. {
  701. INT Compare;
  702. PSETUP_PARTITION_DESCRIPTION Device;
  703. ULONG DeviceCount;
  704. ULONG DeviceIndex;
  705. PSETUP_PARTITION_DESCRIPTION Devices;
  706. BOOL MultipleNonSystem;
  707. PSETUP_PARTITION_DESCRIPTION SecondBest;
  708. PSETUP_PARTITION_DESCRIPTION SelectedPartition;
  709. INT Status;
  710. PSETUP_PARTITION_DESCRIPTION SystemDisk;
  711. Devices = NULL;
  712. if ((Context->DiskPath != NULL) || (Context->PartitionPath != NULL) ||
  713. (Context->DirectoryPath != NULL)) {
  714. fprintf(stderr,
  715. "setup: Autodeploy option is incompatible with a specified "
  716. "install path.\n");
  717. Status = EINVAL;
  718. goto DetermineAutodeployDestinationEnd;
  719. }
  720. //
  721. // Enumerate all eligible setup devices.
  722. //
  723. Status = SetupOsEnumerateDevices(&Devices, &DeviceCount);
  724. if (Status != 0) {
  725. fprintf(stderr, "Error: Failed to enumerate devices.\n");
  726. goto DetermineAutodeployDestinationEnd;
  727. }
  728. //
  729. // Print a description of the eligible devices.
  730. //
  731. if (DeviceCount == 0) {
  732. fprintf(stderr, "Setup found no devices to install to.\n");
  733. Status = ENODEV;
  734. goto DetermineAutodeployDestinationEnd;
  735. }
  736. //
  737. // First find the system disk, or at least try to.
  738. //
  739. SystemDisk = NULL;
  740. for (DeviceIndex = 0; DeviceIndex < DeviceCount; DeviceIndex += 1) {
  741. Device = &(Devices[DeviceIndex]);
  742. if ((Device->Destination->Type == SetupDestinationDisk) &&
  743. ((Device->Flags & SETUP_DEVICE_FLAG_SYSTEM) != 0)) {
  744. SystemDisk = Device;
  745. break;
  746. }
  747. }
  748. MultipleNonSystem = FALSE;
  749. SecondBest = NULL;
  750. SelectedPartition = NULL;
  751. for (DeviceIndex = 0; DeviceIndex < DeviceCount; DeviceIndex += 1) {
  752. Device = &(Devices[DeviceIndex]);
  753. //
  754. // Skip anything that's not a Minoca partition.
  755. //
  756. if ((Device->Destination->Type != SetupDestinationPartition) ||
  757. (Device->Partition.PartitionType != PartitionTypeMinoca)) {
  758. continue;
  759. }
  760. //
  761. // Skip the partition containing the currently running OS.
  762. //
  763. if ((Device->Flags & SETUP_DEVICE_FLAG_SYSTEM) != 0) {
  764. continue;
  765. }
  766. //
  767. // See if this partition is on the system disk. If it is, definitely
  768. // pick it or fail if there are more than one. If it isn't, mark it
  769. // as a possibility, remember if there are more than one, and keep
  770. // looking.
  771. //
  772. Compare = -1;
  773. if (SystemDisk != NULL) {
  774. Compare = memcmp(Device->Partition.DiskId,
  775. SystemDisk->Partition.DiskId,
  776. DISK_IDENTIFIER_SIZE);
  777. }
  778. if (Compare == 0) {
  779. if (SelectedPartition == NULL) {
  780. SelectedPartition = Device;
  781. } else {
  782. Status = ENODEV;
  783. printf("Error: Setup found multiple viable partitions.\n");
  784. goto DetermineAutodeployDestinationEnd;
  785. }
  786. } else {
  787. if (SecondBest == NULL) {
  788. SecondBest = Device;
  789. } else {
  790. MultipleNonSystem = TRUE;
  791. }
  792. }
  793. }
  794. //
  795. // If there was nothing on the system disk but something elsewhere, use it.
  796. // Fail if there were multiple viable non-system partitions.
  797. //
  798. if ((SelectedPartition == NULL) && (SecondBest != NULL)) {
  799. if (MultipleNonSystem != FALSE) {
  800. printf("Error: Setup found multiple viable partitions.\n");
  801. Status = ENODEV;
  802. goto DetermineAutodeployDestinationEnd;
  803. }
  804. SelectedPartition = SecondBest;
  805. }
  806. if (SelectedPartition == NULL) {
  807. printf("Setup found no viable partitions to install to.\n");
  808. Status = ENODEV;
  809. goto DetermineAutodeployDestinationEnd;
  810. }
  811. Context->PartitionPath = SetupCreateDestination(
  812. SelectedPartition->Destination->Type,
  813. SelectedPartition->Destination->Path,
  814. SelectedPartition->Destination->DeviceId);
  815. if (Context->PartitionPath == NULL) {
  816. Status = ENOMEM;
  817. goto DetermineAutodeployDestinationEnd;
  818. }
  819. Status = 0;
  820. DetermineAutodeployDestinationEnd:
  821. if (Devices != NULL) {
  822. SetupDestroyDeviceDescriptions(Devices, DeviceCount);
  823. }
  824. return Status;
  825. }