util.c revision 1.29.2.9 1 /* $NetBSD: util.c,v 1.29.2.9 2022/02/02 04:25:36 msaitoh Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 * or promote products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 /* util.c -- routines that don't really fit anywhere else... */
36
37 #include <assert.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/mount.h>
44 #include <sys/dkio.h>
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/sysctl.h>
49 #include <sys/stat.h>
50 #include <sys/statvfs.h>
51 #include <isofs/cd9660/iso.h>
52 #include <curses.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <dirent.h>
56 #include <util.h>
57 #include "defs.h"
58 #include "md.h"
59 #include "defsizes.h"
60 #include "msg_defs.h"
61 #include "menu_defs.h"
62 #ifdef MD_MAY_SWAP_TO
63 #include <sys/drvctlio.h>
64 #endif
65
66 #define MAX_CD_DEVS 256 /* how many cd drives do we expect to attach */
67 #define ISO_BLKSIZE ISO_DEFAULT_BLOCK_SIZE
68
69 static const char *msg_yes, *msg_no, *msg_all, *msg_some, *msg_none;
70 static int select_menu_width;
71
72 static uint8_t set_status[SET_GROUP_END];
73 #define SET_VALID 0x01
74 #define SET_SELECTED 0x02
75 #define SET_SKIPPED 0x04
76 #define SET_INSTALLED 0x08
77 #define SET_NO_EXTRACT 0x10
78
79 struct tarstats {
80 int nselected;
81 int nfound;
82 int nnotfound;
83 int nerror;
84 int nsuccess;
85 int nskipped;
86 } tarstats;
87
88 distinfo dist_list[] = {
89 #ifdef SET_KERNEL_1_NAME
90 {SET_KERNEL_1_NAME, SET_KERNEL_1, false, MSG_set_kernel_1, NULL},
91 #endif
92 #ifdef SET_KERNEL_2_NAME
93 {SET_KERNEL_2_NAME, SET_KERNEL_2, false, MSG_set_kernel_2, NULL},
94 #endif
95 #ifdef SET_KERNEL_3_NAME
96 {SET_KERNEL_3_NAME, SET_KERNEL_3, false, MSG_set_kernel_3, NULL},
97 #endif
98 #ifdef SET_KERNEL_4_NAME
99 {SET_KERNEL_4_NAME, SET_KERNEL_4, false, MSG_set_kernel_4, NULL},
100 #endif
101 #ifdef SET_KERNEL_5_NAME
102 {SET_KERNEL_5_NAME, SET_KERNEL_5, false, MSG_set_kernel_5, NULL},
103 #endif
104 #ifdef SET_KERNEL_6_NAME
105 {SET_KERNEL_6_NAME, SET_KERNEL_6, false, MSG_set_kernel_6, NULL},
106 #endif
107 #ifdef SET_KERNEL_7_NAME
108 {SET_KERNEL_7_NAME, SET_KERNEL_7, false, MSG_set_kernel_7, NULL},
109 #endif
110 #ifdef SET_KERNEL_8_NAME
111 {SET_KERNEL_8_NAME, SET_KERNEL_8, false, MSG_set_kernel_8, NULL},
112 #endif
113 #ifdef SET_KERNEL_9_NAME
114 {SET_KERNEL_9_NAME, SET_KERNEL_9, false, MSG_set_kernel_9, NULL},
115 #endif
116
117 {"modules", SET_MODULES, false, MSG_set_modules, NULL},
118 {"base", SET_BASE, false, MSG_set_base, NULL},
119 {"etc", SET_ETC, false, MSG_set_system, NULL},
120 {"comp", SET_COMPILER, false, MSG_set_compiler, NULL},
121 {"games", SET_GAMES, false, MSG_set_games, NULL},
122 {"man", SET_MAN_PAGES, false, MSG_set_man_pages, NULL},
123 {"misc", SET_MISC, false, MSG_set_misc, NULL},
124 {"rescue", SET_RESCUE, false, MSG_set_rescue, NULL},
125 {"tests", SET_TESTS, false, MSG_set_tests, NULL},
126 {"text", SET_TEXT_TOOLS, false, MSG_set_text_tools, NULL},
127
128 {NULL, SET_GROUP, false, MSG_set_X11, NULL},
129 {"xbase", SET_X11_BASE, false, MSG_set_X11_base, NULL},
130 {"xcomp", SET_X11_PROG, false, MSG_set_X11_prog, NULL},
131 {"xetc", SET_X11_ETC, false, MSG_set_X11_etc, NULL},
132 {"xfont", SET_X11_FONTS, false, MSG_set_X11_fonts, NULL},
133 {"xserver", SET_X11_SERVERS, false, MSG_set_X11_servers, NULL},
134 {NULL, SET_GROUP_END, false, NULL, NULL},
135
136 #ifdef SET_MD_1_NAME
137 {SET_MD_1_NAME, SET_MD_1, false, MSG_set_md_1, NULL},
138 #endif
139 #ifdef SET_MD_2_NAME
140 {SET_MD_2_NAME, SET_MD_2, false, MSG_set_md_2, NULL},
141 #endif
142 #ifdef SET_MD_3_NAME
143 {SET_MD_3_NAME, SET_MD_3, false, MSG_set_md_3, NULL},
144 #endif
145 #ifdef SET_MD_4_NAME
146 {SET_MD_4_NAME, SET_MD_4, false, MSG_set_md_4, NULL},
147 #endif
148
149 {NULL, SET_GROUP, true, MSG_set_source, NULL},
150 {"syssrc", SET_SYSSRC, true, MSG_set_syssrc, NULL},
151 {"src", SET_SRC, true, MSG_set_src, NULL},
152 {"sharesrc", SET_SHARESRC, true, MSG_set_sharesrc, NULL},
153 {"gnusrc", SET_GNUSRC, true, MSG_set_gnusrc, NULL},
154 {"xsrc", SET_XSRC, true, MSG_set_xsrc, NULL},
155 {"debug", SET_DEBUG, false, MSG_set_debug, NULL},
156 {"xdebug", SET_X11_DEBUG, false, MSG_set_xdebug, NULL},
157 {NULL, SET_GROUP_END, false, NULL, NULL},
158
159 {NULL, SET_LAST, false, NULL, NULL},
160 };
161
162 #define MAX_CD_INFOS 16 /* how many media can be found? */
163 struct cd_info {
164 char device_name[16];
165 char menu[100];
166 };
167 static struct cd_info cds[MAX_CD_INFOS];
168
169 /* flags whether to offer the respective options (depending on helper
170 programs available on install media */
171 int have_raid, have_vnd, have_cgd, have_lvm, have_gpt, have_dk;
172
173 /*
174 * local prototypes
175 */
176
177 static int check_for(unsigned int mode, const char *pathname);
178 static int get_iso9660_volname(int dev, int sess, char *volname);
179 static int get_available_cds(void);
180 static int binary_available(const char *prog);
181
182 void
183 init_set_status(int flags)
184 {
185 static const uint8_t sets_valid[] = {MD_SETS_VALID};
186 static const uint8_t sets_selected_full[] = {MD_SETS_SELECTED};
187 static const uint8_t sets_selected_minimal[] = {MD_SETS_SELECTED_MINIMAL};
188 static const uint8_t sets_selected_nox[] = {MD_SETS_SELECTED_NOX};
189 static const uint8_t *sets_selected;
190 unsigned int nelem_selected;
191 unsigned int i, len;
192 const char *longest;
193
194 if (flags & SFLAG_MINIMAL) {
195 sets_selected = sets_selected_minimal;
196 nelem_selected = __arraycount(sets_selected_minimal);
197 } else if (flags & SFLAG_NOX) {
198 sets_selected = sets_selected_nox;
199 nelem_selected = __arraycount(sets_selected_nox);
200 } else {
201 sets_selected = sets_selected_full;
202 nelem_selected = __arraycount(sets_selected_full);
203 }
204
205 for (i = 0; i < __arraycount(sets_valid); i++)
206 set_status[sets_valid[i]] = SET_VALID;
207 for (i = 0; i < nelem_selected; i++)
208 set_status[sets_selected[i]] |= SET_SELECTED;
209
210 set_status[SET_GROUP] = SET_VALID;
211
212 /* Lookup some strings we need lots of times */
213 msg_yes = msg_string(MSG_Yes);
214 msg_no = msg_string(MSG_No);
215 msg_all = msg_string(MSG_All);
216 msg_some = msg_string(MSG_Some);
217 msg_none = msg_string(MSG_None);
218
219 /* Find longest and use it to determine width of selection menu */
220 len = strlen(msg_no); longest = msg_no;
221 i = strlen(msg_yes); if (i > len) {len = i; longest = msg_yes; }
222 i = strlen(msg_all); if (i > len) {len = i; longest = msg_all; }
223 i = strlen(msg_some); if (i > len) {len = i; longest = msg_some; }
224 i = strlen(msg_none); if (i > len) {len = i; longest = msg_none; }
225 select_menu_width = snprintf(NULL, 0, "%-30s %s", "", longest);
226
227 /* Give the md code a chance to choose the right kernel, etc. */
228 md_init_set_status(flags);
229 }
230
231 int
232 dir_exists_p(const char *path)
233 {
234
235 return file_mode_match(path, S_IFDIR);
236 }
237
238 int
239 file_exists_p(const char *path)
240 {
241
242 return file_mode_match(path, S_IFREG);
243 }
244
245 int
246 file_mode_match(const char *path, unsigned int mode)
247 {
248 struct stat st;
249
250 return (stat(path, &st) == 0 && (st.st_mode & S_IFMT) == mode);
251 }
252
253 /* return ram size in MB */
254 uint64_t
255 get_ramsize(void)
256 {
257 uint64_t ramsize;
258 size_t len = sizeof ramsize;
259 int mib[2] = {CTL_HW, HW_PHYSMEM64};
260
261 sysctl(mib, 2, &ramsize, &len, NULL, 0);
262
263 /* Find out how many Megs ... round up. */
264 return (ramsize + MEG - 1) / MEG;
265 }
266
267 void
268 run_makedev(void)
269 {
270 char *owd;
271
272 msg_display_add("\n\n");
273 msg_display_add(MSG_makedev);
274
275 owd = getcwd(NULL, 0);
276
277 /* make /dev, in case the user didn't extract it. */
278 make_target_dir("/dev");
279 target_chdir_or_die("/dev");
280 run_program(0, "/bin/sh MAKEDEV all");
281
282 chdir(owd);
283 free(owd);
284 }
285
286 /*
287 * Performs in-place replacement of a set of patterns in a file that lives
288 * inside the installed system. The patterns must be separated by a semicolon.
289 * For example:
290 *
291 * replace("/etc/some-file.conf", "s/prop1=NO/prop1=YES/;s/foo/bar/");
292 */
293 void
294 replace(const char *path, const char *patterns, ...)
295 {
296 char *spatterns;
297 va_list ap;
298
299 va_start(ap, patterns);
300 vasprintf(&spatterns, patterns, ap);
301 va_end(ap);
302 if (spatterns == NULL)
303 err(1, "vasprintf(&spatterns, \"%s\", ...)", patterns);
304
305 run_program(RUN_CHROOT, "sed -an -e '%s;H;$!d;g;w %s' %s", spatterns,
306 path, path);
307
308 free(spatterns);
309 }
310
311 static int
312 floppy_fetch(const char *set_name)
313 {
314 char post[4];
315 msg errmsg;
316 int menu;
317 int status;
318 const char *write_mode = ">";
319
320 strcpy(post, "aa");
321
322 errmsg = "";
323 menu = MENU_fdok;
324 for (;;) {
325 umount_mnt2();
326 msg_display(errmsg);
327 msg_fmt_display_add(MSG_fdmount, "%s%s", set_name, post);
328 process_menu(menu, &status);
329 if (status != SET_CONTINUE)
330 return status;
331 menu = MENU_fdremount;
332 errmsg = MSG_fdremount;
333 if (run_program(0, "/sbin/mount -r -t %s %s /mnt2",
334 fd_type, fd_dev))
335 continue;
336 mnt2_mounted = 1;
337 errmsg = MSG_fdnotfound;
338
339 /* Display this because it might take a while.... */
340 if (run_program(RUN_DISPLAY,
341 "sh -c '/bin/cat /mnt2/%s.%s %s %s/%s/%s%s'",
342 set_name, post, write_mode,
343 target_prefix(), xfer_dir, set_name,
344 set_postfix(set_name)))
345 /* XXX: a read error will give a corrupt file! */
346 continue;
347
348 /* We got that file, advance to next fragment */
349 if (post[1] < 'z')
350 post[1]++;
351 else
352 post[1] = 'a', post[0]++;
353 write_mode = ">>";
354 errmsg = "";
355 menu = MENU_fdok;
356 }
357 }
358
359 /*
360 * Load files from floppy. Requires a /mnt2 directory for mounting them.
361 */
362 int
363 get_via_floppy(void)
364 {
365 int rv = -1;
366
367 process_menu(MENU_floppysource, &rv);
368 if (rv == SET_RETRY)
369 return SET_RETRY;
370
371 fetch_fn = floppy_fetch;
372
373 /* Set ext_dir for absolute path. */
374 snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(), xfer_dir);
375 snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(), xfer_dir);
376
377 return SET_OK;
378 }
379
380 /*
381 * Get the volume name of a ISO9660 file system
382 */
383 static int
384 get_iso9660_volname(int dev, int sess, char *volname)
385 {
386 int blkno, error, last;
387 char buf[ISO_BLKSIZE];
388 struct iso_volume_descriptor *vd = NULL;
389 struct iso_primary_descriptor *pd = NULL;
390
391 for (blkno = sess+16; blkno < sess+16+100; blkno++) {
392 error = pread(dev, buf, ISO_BLKSIZE, blkno*ISO_BLKSIZE);
393 if (error == -1)
394 return -1;
395 vd = (struct iso_volume_descriptor *)&buf;
396 if (memcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
397 return -1;
398 if (isonum_711((const unsigned char *)&vd->type)
399 == ISO_VD_PRIMARY) {
400 pd = (struct iso_primary_descriptor*)buf;
401 strncpy(volname, pd->volume_id, sizeof pd->volume_id);
402 last = sizeof pd->volume_id-1;
403 while (last >= 0
404 && (volname[last] == ' ' || volname[last] == 0))
405 last--;
406 volname[last+1] = 0;
407 return 0;
408 }
409 }
410 return -1;
411 }
412
413 /*
414 * Local state while iterating CDs and collecting volumes
415 */
416 struct get_available_cds_state {
417 size_t num_mounted;
418 struct statvfs *mounted;
419 struct cd_info *info;
420 size_t count;
421 };
422
423 /*
424 * Callback function: if this is a CD, enumerate all volumes on it
425 */
426 static bool
427 get_available_cds_helper(void *arg, const char *device)
428 {
429 struct get_available_cds_state *state = arg;
430 char dname[16], tname[16], volname[80], *t;
431 struct disklabel label;
432 int part, dev, error, sess, ready, tlen;
433
434 if (!is_cdrom_device(device, false))
435 return true;
436
437 sprintf(dname, "/dev/r%s%c", device, 'a'+RAW_PART);
438 tlen = sprintf(tname, "/dev/%s", device);
439
440 /* check if this is mounted already */
441 for (size_t i = 0; i < state->num_mounted; i++) {
442 if (strncmp(state->mounted[i].f_mntfromname, tname, tlen)
443 == 0) {
444 t = state->mounted[i].f_mntfromname + tlen;
445 if (t[0] >= 'a' && t[0] <= 'z' && t[1] == 0)
446 return true;
447 }
448 }
449
450 dev = open(dname, O_RDONLY, 0);
451 if (dev == -1)
452 return true;
453
454 ready = 0;
455 error = ioctl(dev, DIOCTUR, &ready);
456 if (error != 0 || ready == 0) {
457 close(dev);
458 return true;
459 }
460 error = ioctl(dev, DIOCGDINFO, &label);
461 close(dev);
462 if (error != 0)
463 return true;
464
465 for (part = 0; part < label.d_npartitions; part++) {
466
467 if (label.d_partitions[part].p_fstype == FS_UNUSED
468 || label.d_partitions[part].p_size == 0)
469 continue;
470
471 if (label.d_partitions[part].p_fstype == FS_ISO9660) {
472 sess = label.d_partitions[part].p_cdsession;
473 sprintf(dname, "/dev/r%s%c", device, 'a'+part);
474 dev = open(dname, O_RDONLY, 0);
475 if (dev == -1)
476 continue;
477 error = get_iso9660_volname(dev, sess, volname);
478 close(dev);
479 if (error)
480 continue;
481 sprintf(state->info->device_name,
482 "%s%c", device, 'a'+part);
483 sprintf(state->info->menu, "%s (%s)",
484 state->info->device_name, volname);
485 } else {
486 /*
487 * All install CDs use partition
488 * a for the sets.
489 */
490 if (part > 0)
491 continue;
492 sprintf(state->info->device_name,
493 "%s%c", device, 'a'+part);
494 strcpy(state->info->menu, state->info->device_name);
495 }
496 state->info++;
497 if (++state->count >= MAX_CD_INFOS)
498 return false;
499 }
500
501 return true;
502 }
503
504 /*
505 * Get a list of all available CD media (not drives!), return
506 * the number of entries collected.
507 */
508 static int
509 get_available_cds(void)
510 {
511 struct get_available_cds_state data;
512 int n, m;
513
514 memset(&data, 0, sizeof data);
515 data.info = cds;
516
517 n = getvfsstat(NULL, 0, ST_NOWAIT);
518 if (n > 0) {
519 data.mounted = calloc(n, sizeof(*data.mounted));
520 m = getvfsstat(data.mounted, n*sizeof(*data.mounted),
521 ST_NOWAIT);
522 assert(m >= 0 && m <= n);
523 data.num_mounted = m;
524 }
525
526 enumerate_disks(&data, get_available_cds_helper);
527
528 free(data.mounted);
529
530 return data.count;
531 }
532
533 static int
534 cd_has_sets(void)
535 {
536
537 /* sanity check */
538 if (cdrom_dev[0] == 0)
539 return 0;
540
541 /* Mount it */
542 if (run_program(RUN_SILENT, "/sbin/mount -rt cd9660 /dev/%s /mnt2",
543 cdrom_dev) != 0)
544 return 0;
545
546 mnt2_mounted = 1;
547
548 snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", "/mnt2", set_dir_bin);
549 snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", "/mnt2", set_dir_src);
550 return dir_exists_p(ext_dir_bin);
551 }
552
553 /*
554 * Check whether we can remove the boot media.
555 * If it is not a local filesystem, return -1.
556 * If we can not decide for sure (can not tell MD content from plain ffs
557 * on hard disk, for example), return 0.
558 * If it is a CD/DVD, return 1.
559 */
560 int
561 boot_media_still_needed(void)
562 {
563 struct statvfs sb;
564
565 if (statvfs("/", &sb) == 0) {
566 if (!(sb.f_flag & ST_LOCAL))
567 return -1;
568 if (strcmp(sb.f_fstypename, MOUNT_CD9660) == 0
569 || strcmp(sb.f_fstypename, MOUNT_UDF) == 0)
570 return 1;
571 }
572
573 return 0;
574 }
575
576 bool
577 root_is_read_only(void)
578 {
579 struct statvfs sb;
580
581 if (statvfs("/", &sb) == 0)
582 return sb.f_flag & ST_RDONLY;
583
584 return false;
585 }
586
587 /*
588 * Get from a CDROM distribution.
589 * Also used on "installation using bootable install media"
590 * as the default option in the "distmedium" menu.
591 */
592 int
593 get_via_cdrom(void)
594 {
595 menu_ent cd_menu[MAX_CD_INFOS];
596 struct stat sb;
597 int rv, num_cds, menu_cd, i, selected_cd = 0;
598 int mib[2];
599 char rootdev[SSTRSIZE] = "";
600 size_t varlen;
601
602 /* If root is not md(4) and we have set dir, skip this step. */
603 mib[0] = CTL_KERN;
604 mib[1] = KERN_ROOT_DEVICE;
605 varlen = sizeof(rootdev);
606 (void)sysctl(mib, 2, rootdev, &varlen, NULL, 0);
607 if (stat(set_dir_bin, &sb) == 0 && S_ISDIR(sb.st_mode) &&
608 strncmp("md", rootdev, 2) != 0) {
609 strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
610 strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
611 return SET_OK;
612 }
613
614 memset(cd_menu, 0, sizeof(cd_menu));
615 num_cds = get_available_cds();
616 if (num_cds <= 0) {
617 msg_display(MSG_No_cd_found);
618 cdrom_dev[0] = 0;
619 } else if (num_cds == 1) {
620 /* single CD found, check for sets on it */
621 strcpy(cdrom_dev, cds[0].device_name);
622 if (cd_has_sets())
623 return SET_OK;
624 } else {
625 for (i = 0; i< num_cds; i++) {
626 cd_menu[i].opt_name = cds[i].menu;
627 cd_menu[i].opt_flags = OPT_EXIT;
628 cd_menu[i].opt_action = set_menu_select;
629 }
630 /* create a menu offering available choices */
631 menu_cd = new_menu(MSG_Available_cds,
632 cd_menu, num_cds, -1, 4, 0, 0,
633 MC_SCROLL | MC_NOEXITOPT,
634 NULL, NULL, NULL, NULL, NULL);
635 if (menu_cd == -1)
636 return SET_RETRY;
637 msg_display(MSG_ask_cd);
638 process_menu(menu_cd, &selected_cd);
639 free_menu(menu_cd);
640 strcpy(cdrom_dev, cds[selected_cd].device_name);
641 if (cd_has_sets())
642 return SET_OK;
643 }
644
645 if (num_cds >= 1 && mnt2_mounted) {
646 umount_mnt2();
647 hit_enter_to_continue(MSG_cd_path_not_found, NULL);
648 }
649
650 /* ask for paths on the CD */
651 rv = -1;
652 process_menu(MENU_cdromsource, &rv);
653 if (rv == SET_RETRY || rv == SET_ABANDON)
654 return rv;
655
656 if (cd_has_sets())
657 return SET_OK;
658
659 return SET_RETRY;
660 }
661
662
663 /*
664 * Get from a pathname inside an unmounted local filesystem
665 * (e.g., where sets were preloaded onto a local DOS partition)
666 */
667 int
668 get_via_localfs(void)
669 {
670 int rv = -1;
671
672 /* Get device, filesystem, and filepath */
673 process_menu (MENU_localfssource, &rv);
674 if (rv == SET_RETRY)
675 return SET_RETRY;
676
677 /* Mount it */
678 if (run_program(0, "/sbin/mount -rt %s /dev/%s /mnt2",
679 localfs_fs, localfs_dev))
680 return SET_RETRY;
681
682 mnt2_mounted = 1;
683
684 snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s/%s",
685 "/mnt2", localfs_dir, set_dir_bin);
686 snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s/%s",
687 "/mnt2", localfs_dir, set_dir_src);
688
689 return SET_OK;
690 }
691
692 /*
693 * Get from an already-mounted pathname.
694 */
695
696 int
697 get_via_localdir(void)
698 {
699 int rv = -1;
700
701 /* Get filepath */
702 process_menu(MENU_localdirsource, &rv);
703 if (rv == SET_RETRY)
704 return SET_RETRY;
705
706 /*
707 * We have to have an absolute path ('cos pax runs in a
708 * different directory), make it so.
709 */
710 snprintf(ext_dir_bin, sizeof ext_dir_bin, "/%s/%s", localfs_dir, set_dir_bin);
711 snprintf(ext_dir_src, sizeof ext_dir_src, "/%s/%s", localfs_dir, set_dir_src);
712
713 return SET_OK;
714 }
715
716
717 /*
718 * Support for custom distribution fetches / unpacks.
719 */
720
721 unsigned int
722 set_X11_selected(void)
723 {
724 int i;
725
726 for (i = SET_X11_FIRST; ++i < SET_X11_LAST;)
727 if (set_status[i] & SET_SELECTED)
728 return 1;
729 return 0;
730 }
731
732 unsigned int
733 get_kernel_set(void)
734 {
735 int i;
736
737 for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
738 if (set_status[i] & SET_SELECTED)
739 return i;
740 return SET_NONE;
741 }
742
743 void
744 set_kernel_set(unsigned int kernel_set)
745 {
746 int i;
747
748 /* only one kernel set is allowed */
749 for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
750 set_status[i] &= ~SET_SELECTED;
751 set_status[kernel_set] |= SET_SELECTED;
752 }
753
754 void
755 set_noextract_set(unsigned int set)
756 {
757
758 set_status[set] |= SET_NO_EXTRACT;
759 }
760
761 static int
762 set_toggle(menudesc *menu, void *arg)
763 {
764 distinfo **distp = arg;
765 int set = distp[menu->cursel]->set;
766
767 if (set > SET_KERNEL_FIRST && set < SET_KERNEL_LAST &&
768 !(set_status[set] & SET_SELECTED))
769 set_kernel_set(set);
770 else
771 set_status[set] ^= SET_SELECTED;
772 return 0;
773 }
774
775 static int
776 set_all_none(menudesc *menu, void *arg, int set, int clr)
777 {
778 distinfo **distp = arg;
779 distinfo *dist = *distp;
780 int nested;
781
782 for (nested = 0; dist->set != SET_GROUP_END || nested--; dist++) {
783 if (dist->set == SET_GROUP) {
784 nested++;
785 continue;
786 }
787 set_status[dist->set] = (set_status[dist->set] & ~clr) | set;
788 }
789 return 0;
790 }
791
792 static int
793 set_all(menudesc *menu, void *arg)
794 {
795 return set_all_none(menu, arg, SET_SELECTED, 0);
796 }
797
798 static int
799 set_none(menudesc *menu, void *arg)
800 {
801 return set_all_none(menu, arg, 0, SET_SELECTED);
802 }
803
804 static void
805 set_label(menudesc *menu, int opt, void *arg)
806 {
807 distinfo **distp = arg;
808 distinfo *dist = distp[opt];
809 const char *selected;
810 const char *desc;
811 int nested;
812
813 desc = dist->desc;
814
815 if (dist->set != SET_GROUP)
816 selected = set_status[dist->set] & SET_SELECTED ? msg_yes : msg_no;
817 else {
818 /* sub menu - display None/Some/All */
819 nested = 0;
820 selected = "unknown";
821 while ((++dist)->set != SET_GROUP_END || nested--) {
822 if (dist->set == SET_GROUP) {
823 nested++;
824 continue;
825 }
826 if (!(set_status[dist->set] & SET_VALID))
827 continue;
828 if (set_status[dist->set] & SET_SELECTED) {
829 if (selected == msg_none) {
830 selected = msg_some;
831 break;
832 }
833 selected = msg_all;
834 } else {
835 if (selected == msg_all) {
836 selected = msg_some;
837 break;
838 }
839 selected = msg_none;
840 }
841 }
842 }
843
844 wprintw(menu->mw, "%-30s %s", msg_string(desc), selected);
845 }
846
847 static int set_sublist(menudesc *menu, void *arg);
848
849 static int
850 initialise_set_menu(distinfo *dist, menu_ent *me, distinfo **de, int all_none)
851 {
852 int set;
853 int sets;
854 int nested;
855
856 for (sets = 0; ; dist++) {
857 set = dist->set;
858 if (set == SET_LAST || set == SET_GROUP_END)
859 break;
860 if (!(set_status[set] & SET_VALID))
861 continue;
862 *de = dist;
863 memset(me, 0, sizeof(*me));
864 if (set != SET_GROUP)
865 me->opt_action = set_toggle;
866 else {
867 /* Collapse sublist */
868 nested = 0;
869 while ((++dist)->set != SET_GROUP_END || nested--) {
870 if (dist->set == SET_GROUP)
871 nested++;
872 }
873 me->opt_action = set_sublist;
874 }
875 sets++;
876 de++;
877 me++;
878 }
879
880 if (all_none) {
881 me->opt_name = MSG_select_all;
882 me->opt_action = set_all;
883 me++;
884 me->opt_name = MSG_select_none;
885 me->opt_action = set_none;
886 sets += 2;
887 }
888
889 return sets;
890 }
891
892 static int
893 set_sublist(menudesc *menu, void *arg)
894 {
895 distinfo *de[SET_LAST];
896 menu_ent me[SET_LAST];
897 distinfo **dist = arg;
898 int menu_no;
899 int sets;
900
901 memset(me, 0, sizeof(me));
902 sets = initialise_set_menu(dist[menu->cursel] + 1, me, de, 1);
903
904 menu_no = new_menu(NULL, me, sets, 20, 10, 0, select_menu_width,
905 MC_SUBMENU | MC_SCROLL | MC_DFLTEXIT,
906 NULL, set_label, NULL, NULL,
907 MSG_install_selected_sets);
908
909 process_menu(menu_no, de);
910 free_menu(menu_no);
911
912 return 0;
913 }
914
915 void
916 customise_sets(void)
917 {
918 distinfo *de[SET_LAST];
919 menu_ent me[SET_LAST];
920 int sets;
921 int menu_no;
922
923 msg_display(MSG_cur_distsets);
924 msg_table_add(MSG_cur_distsets_header);
925
926 memset(me, 0, sizeof(me));
927 sets = initialise_set_menu(dist_list, me, de, 0);
928
929 menu_no = new_menu(NULL, me, sets, 0, 5, 0, select_menu_width,
930 MC_SCROLL | MC_NOBOX | MC_DFLTEXIT | MC_NOCLEAR,
931 NULL, set_label, NULL, NULL,
932 MSG_install_selected_sets);
933
934 process_menu(menu_no, de);
935 free_menu(menu_no);
936 }
937
938 /*
939 * Extract_file **REQUIRES** an absolute path in ext_dir. Any code
940 * that sets up xfer_dir for use by extract_file needs to put in the
941 * full path name to the directory.
942 */
943
944 int
945 extract_file(distinfo *dist, int update)
946 {
947 const char *dest_dir = NULL;
948
949 if (update && (dist->set == SET_ETC || dist->set == SET_X11_ETC)) {
950 dest_dir = "/.sysinst";
951 make_target_dir(dest_dir);
952 } else if (dist->set == SET_PKGSRC)
953 dest_dir = "/usr";
954 else
955 dest_dir = "/";
956
957 return extract_file_to(dist, update, dest_dir, NULL, true);
958 }
959
960 int
961 extract_file_to(distinfo *dist, int update, const char *dest_dir,
962 const char *extr_pattern, bool do_stats)
963 {
964 char path[STRSIZE];
965 char *owd;
966 int rval;
967
968 /* If we might need to tidy up, ensure directory exists */
969 if (fetch_fn != NULL)
970 make_target_dir(xfer_dir);
971
972 (void)snprintf(path, sizeof path, "%s/%s%s",
973 ext_dir_for_set(dist->name), dist->name, set_postfix(dist->name));
974
975 owd = getcwd(NULL, 0);
976
977 /* Do we need to fetch the file now? */
978 if (fetch_fn != NULL) {
979 rval = fetch_fn(dist->name);
980 if (rval != SET_OK)
981 return rval;
982 }
983
984 /* check tarfile exists */
985 if (!file_exists_p(path)) {
986
987 #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
988 /*
989 * Update path to use dist->name truncated to the first eight
990 * characters and check again
991 */
992 (void)snprintf(path, sizeof path,
993 "%s/%.8s%.4s", /* 4 as includes '.' */
994 ext_dir_for_set(dist->name), dist->name,
995 set_postfix(dist->name));
996
997 if (!file_exists_p(path)) {
998 #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
999 if (do_stats)
1000 tarstats.nnotfound++;
1001
1002 char *err = str_arg_subst(msg_string(MSG_notarfile),
1003 1, &dist->name);
1004 hit_enter_to_continue(err, NULL);
1005 free(err);
1006 free(owd);
1007 return SET_RETRY;
1008 }
1009 #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
1010 }
1011 #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
1012
1013 if (do_stats)
1014 tarstats.nfound++;
1015 /* cd to the target root. */
1016 target_chdir_or_die(dest_dir);
1017
1018 /*
1019 * /usr/X11R7/lib/X11/xkb/symbols/pc was a directory in 5.0
1020 * but is a file in 5.1 and beyond, so on upgrades we need to
1021 * delete it before extracting the xbase set.
1022 */
1023 if (update && dist->set == SET_X11_BASE)
1024 run_program(0, "rm -rf usr/X11R7/lib/X11/xkb/symbols/pc");
1025
1026 /* now extract set files into "./". */
1027 if (extr_pattern != NULL) {
1028 rval = run_program(RUN_DISPLAY | RUN_PROGRESS,
1029 "progress -zf %s tar --chroot "
1030 TAR_EXTRACT_FLAGS " - '%s'",
1031 path, extr_pattern);
1032 } else {
1033 rval = run_program(RUN_DISPLAY | RUN_PROGRESS,
1034 "progress -zf %s tar --chroot "
1035 TAR_EXTRACT_FLAGS " -", path);
1036 }
1037
1038 chdir(owd);
1039 free(owd);
1040
1041 /* Check rval for errors and give warning. */
1042 if (rval != 0) {
1043 if (do_stats)
1044 tarstats.nerror++;
1045 msg_fmt_display(MSG_tarerror, "%s", path);
1046 hit_enter_to_continue(NULL, NULL);
1047 return SET_RETRY;
1048 }
1049
1050 if (fetch_fn != NULL && clean_xfer_dir) {
1051 run_program(0, "rm %s", path);
1052 /* Plausibly we should unlink an empty xfer_dir as well */
1053 }
1054
1055 set_status[dist->set] |= SET_INSTALLED;
1056 if (do_stats)
1057 tarstats.nsuccess++;
1058 return SET_OK;
1059 }
1060
1061 static void
1062 skip_set(distinfo *dist, int skip_type)
1063 {
1064 int nested;
1065 int set;
1066
1067 nested = 0;
1068 while ((++dist)->set != SET_GROUP_END || nested--) {
1069 set = dist->set;
1070 if (set == SET_GROUP) {
1071 nested++;
1072 continue;
1073 }
1074 if (set == SET_LAST)
1075 break;
1076 if (set_status[set] == (SET_SELECTED | SET_VALID))
1077 set_status[set] |= SET_SKIPPED;
1078 tarstats.nskipped++;
1079 }
1080 }
1081
1082 distinfo*
1083 get_set_distinfo(int opt)
1084 {
1085 distinfo *dist;
1086 int set;
1087
1088 for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
1089 if (set != opt)
1090 continue;
1091 if (dist->name == NULL)
1092 continue;
1093 if ((set_status[set] & (SET_VALID | SET_SELECTED))
1094 != (SET_VALID | SET_SELECTED))
1095 continue;
1096 return dist;
1097 }
1098
1099 return NULL;
1100 }
1101
1102
1103 /*
1104 * Get and unpack the distribution.
1105 * Show success_msg if installation completes.
1106 * Otherwise show failure_msg and wait for the user to ack it before continuing.
1107 * success_msg and failure_msg must both be 0-adic messages.
1108 */
1109 int
1110 get_and_unpack_sets(int update, msg setupdone_msg, msg success_msg, msg failure_msg)
1111 {
1112 distinfo *dist;
1113 int status;
1114 int set, olderror, oldfound;
1115 bool entropy_loaded = false;
1116
1117 /* Ensure mountpoint for distribution files exists in current root. */
1118 (void)mkdir("/mnt2", S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
1119 if (script)
1120 (void)fprintf(script, "mkdir -m 755 /mnt2\n");
1121
1122 /* reset failure/success counters */
1123 memset(&tarstats, 0, sizeof(tarstats));
1124
1125 /* Find out which files to "get" if we get files. */
1126
1127 /* Accurately count selected sets */
1128 for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
1129 if (dist->name == NULL)
1130 continue;
1131 if (set_status[set] & SET_NO_EXTRACT)
1132 continue;
1133 if ((set_status[set] & (SET_VALID | SET_SELECTED))
1134 == (SET_VALID | SET_SELECTED))
1135 tarstats.nselected++;
1136 }
1137
1138 status = SET_RETRY;
1139 for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
1140 if (dist->name == NULL)
1141 continue;
1142 if (set_status[set] != (SET_VALID | SET_SELECTED))
1143 continue;
1144
1145 /* save stats, in case we will retry */
1146 oldfound = tarstats.nfound;
1147 olderror = tarstats.nerror;
1148
1149 if (status != SET_OK) {
1150 /* This might force a redraw.... */
1151 clearok(curscr, 1);
1152 touchwin(stdscr);
1153 wrefresh(stdscr);
1154 /* Sort out the location of the set files */
1155 do {
1156 umount_mnt2();
1157 msg_fmt_display(MSG_distmedium, "%d%d%s",
1158 tarstats.nselected,
1159 tarstats.nsuccess + tarstats.nskipped,
1160 dist->name);
1161 fetch_fn = NULL;
1162 process_menu(MENU_distmedium, &status);
1163 } while (status == SET_RETRY);
1164
1165 if (status == SET_SKIP) {
1166 set_status[set] |= SET_SKIPPED;
1167 tarstats.nskipped++;
1168 continue;
1169 }
1170 if (status == SET_SKIP_GROUP) {
1171 skip_set(dist, status);
1172 continue;
1173 }
1174 if (status != SET_OK) {
1175 hit_enter_to_continue(failure_msg, NULL);
1176 return 1;
1177 }
1178 }
1179
1180 if (set_status[set] & SET_NO_EXTRACT)
1181 continue;
1182
1183 /* Try to extract this set */
1184 status = extract_file(dist, update);
1185 if (status == SET_RETRY) {
1186 /* do this set again */
1187 dist--;
1188 /* and reset statistics to what we had before this
1189 * set */
1190 tarstats.nfound = oldfound;
1191 tarstats.nerror = olderror;
1192 }
1193 }
1194
1195 #ifdef MD_SET_EXTRACT_FINALIZE
1196 MD_SET_EXTRACT_FINALIZE(update);
1197 #endif
1198
1199 if (tarstats.nerror == 0 && tarstats.nsuccess == tarstats.nselected) {
1200 msg_display(MSG_endtarok);
1201 /* Give user a chance to see the success message */
1202 sleep(1);
1203 } else {
1204 /* We encountered errors. Let the user know. */
1205 msg_fmt_display(MSG_endtar, "%d%d%d%d%d%d",
1206 tarstats.nselected, tarstats.nnotfound, tarstats.nskipped,
1207 tarstats.nfound, tarstats.nsuccess, tarstats.nerror);
1208 hit_enter_to_continue(NULL, NULL);
1209 }
1210
1211 /*
1212 * postinstall needs to be run after extracting all sets, because
1213 * otherwise /var/db/obsolete will only have current information
1214 * from the base, comp, and etc sets.
1215 */
1216 if (update && (set_status[SET_ETC] & SET_INSTALLED)) {
1217 int oldsendmail;
1218 oldsendmail = run_program(RUN_DISPLAY | RUN_CHROOT |
1219 RUN_ERROR_OK | RUN_PROGRESS,
1220 "/usr/sbin/postinstall -s /.sysinst -d / check mailerconf");
1221 if (oldsendmail == 1) {
1222 msg_display(MSG_oldsendmail);
1223 if (ask_yesno(NULL)) {
1224 run_program(RUN_DISPLAY | RUN_CHROOT,
1225 "/usr/sbin/postinstall -s /.sysinst -d / fix mailerconf");
1226 }
1227 }
1228 run_program(RUN_DISPLAY | RUN_CHROOT,
1229 "/usr/sbin/postinstall -s /.sysinst -d / fix");
1230
1231 /* Don't discard the system's old entropy if any */
1232 run_program(RUN_CHROOT | RUN_SILENT,
1233 "/etc/rc.d/random_seed start");
1234 entropy_loaded = true;
1235 }
1236
1237 /* Configure the system */
1238 if (set_status[SET_BASE] & SET_INSTALLED)
1239 run_makedev();
1240
1241 if (!update) {
1242 struct stat sb1, sb2;
1243
1244 if (stat(target_expand("/"), &sb1) == 0
1245 && stat(target_expand("/var"), &sb2) == 0
1246 && sb1.st_dev != sb2.st_dev) {
1247 add_rc_conf("random_file=/etc/entropy-file\n");
1248 if (target_file_exists_p("/boot.cfg")) {
1249 run_program(RUN_CHROOT|RUN_FATAL,
1250 "sh -c 'sed -e s./var/db/./etc/. "
1251 "< /boot.cfg "
1252 "> /tmp/boot.cfg.tmp'");
1253 mv_within_target_or_die("/tmp/boot.cfg.tmp",
1254 "/boot.cfg");
1255
1256 }
1257 }
1258
1259 #ifdef MD_BOOT_CFG_FINALIZE
1260 if (target_file_exists_p("/boot.cfg")) {
1261 MD_BOOT_CFG_FINALIZE("/boot.cfg");
1262 }
1263 #endif
1264
1265 /* Save keyboard type */
1266 save_kb_encoding();
1267
1268 /* Other configuration. */
1269 mnt_net_config();
1270 }
1271
1272 /* Mounted dist dir? */
1273 umount_mnt2();
1274
1275 /* Save entropy -- on some systems it's ~all we'll ever get */
1276 if (!update || entropy_loaded)
1277 run_program(RUN_SILENT | RUN_CHROOT | RUN_ERROR_OK,
1278 "/etc/rc.d/random_seed stop");
1279 /* Install/Upgrade complete ... reboot or exit to script */
1280 hit_enter_to_continue(success_msg, NULL);
1281 return 0;
1282 }
1283
1284 void
1285 umount_mnt2(void)
1286 {
1287 if (!mnt2_mounted)
1288 return;
1289 run_program(RUN_SILENT, "/sbin/umount /mnt2");
1290 mnt2_mounted = 0;
1291 }
1292
1293
1294 /*
1295 * Do a quick sanity check that the target can reboot.
1296 * return 1 if everything OK, 0 if there is a problem.
1297 * Uses a table of files we expect to find after a base install/upgrade.
1298 */
1299
1300 /* test flag and pathname to check for after unpacking. */
1301 struct check_table { unsigned int mode; const char *path;} checks[] = {
1302 { S_IFREG, "/netbsd" },
1303 { S_IFDIR, "/etc" },
1304 { S_IFREG, "/etc/fstab" },
1305 { S_IFREG, "/sbin/init" },
1306 { S_IFREG, "/bin/sh" },
1307 { S_IFREG, "/etc/rc" },
1308 { S_IFREG, "/etc/rc.subr" },
1309 { S_IFREG, "/etc/rc.conf" },
1310 { S_IFDIR, "/dev" },
1311 { S_IFCHR, "/dev/console" },
1312 /* XXX check for rootdev in target /dev? */
1313 { S_IFREG, "/sbin/fsck" },
1314 { S_IFREG, "/sbin/fsck_ffs" },
1315 { S_IFREG, "/sbin/mount" },
1316 { S_IFREG, "/sbin/mount_ffs" },
1317 { S_IFREG, "/sbin/mount_nfs" },
1318 #if defined(DEBUG) || defined(DEBUG_CHECK)
1319 { S_IFREG, "/foo/bar" }, /* bad entry to exercise warning */
1320 #endif
1321 { 0, 0 }
1322
1323 };
1324
1325 /*
1326 * Check target for a single file.
1327 */
1328 static int
1329 check_for(unsigned int mode, const char *pathname)
1330 {
1331 int found;
1332
1333 found = (target_test(mode, pathname) == 0);
1334 if (found == 0)
1335 msg_fmt_display(MSG_rootmissing, "%s", pathname);
1336 return found;
1337 }
1338
1339 /*
1340 * Check that all the files in check_table are present in the
1341 * target root. Warn if not found.
1342 */
1343 int
1344 sanity_check(void)
1345 {
1346 int target_ok = 1;
1347 struct check_table *p;
1348
1349 for (p = checks; p->path; p++) {
1350 target_ok = target_ok && check_for(p->mode, p->path);
1351 }
1352 if (target_ok)
1353 return 0;
1354
1355 /* Uh, oh. Something's missing. */
1356 hit_enter_to_continue(MSG_badroot, NULL);
1357 return 1;
1358 }
1359
1360 /*
1361 * Some globals to pass things back from callbacks
1362 */
1363 static char zoneinfo_dir[STRSIZE];
1364 static int zonerootlen;
1365 static char *tz_selected; /* timezonename (relative to share/zoneinfo */
1366 const char *tz_default; /* UTC, or whatever /etc/localtime points to */
1367 static char tz_env[STRSIZE];
1368 static int save_cursel, save_topline;
1369 static int time_menu = -1;
1370
1371 static void
1372 update_time_display(void)
1373 {
1374 time_t t;
1375 struct tm *tm;
1376 char cur_time[STRSIZE], *p;
1377
1378 t = time(NULL);
1379 tm = localtime(&t);
1380 strlcpy(cur_time, safectime(&t), sizeof cur_time);
1381 p = strchr(cur_time, '\n');
1382 if (p != NULL)
1383 *p = 0;
1384
1385 msg_clear();
1386 msg_fmt_table_add(MSG_choose_timezone, "%s%s%s%s",
1387 tz_default, tz_selected, cur_time, tm ? tm->tm_zone : "?");
1388 }
1389
1390 /*
1391 * Callback from timezone menu
1392 */
1393 static int
1394 set_tz_select(menudesc *m, void *arg)
1395 {
1396 char *new;
1397
1398 if (m && strcmp(tz_selected, m->opts[m->cursel].opt_name) != 0) {
1399 /* Change the displayed timezone */
1400 new = strdup(m->opts[m->cursel].opt_name);
1401 if (new == NULL)
1402 return 0;
1403 free(tz_selected);
1404 tz_selected = new;
1405 snprintf(tz_env, sizeof tz_env, "%.*s%s",
1406 zonerootlen, zoneinfo_dir, tz_selected);
1407 setenv("TZ", tz_env, 1);
1408 }
1409 if (m)
1410 /* Warp curser to 'Exit' line on menu */
1411 m->cursel = -1;
1412
1413 update_time_display();
1414 if (time_menu >= 1) {
1415 WINDOW *w = get_menudesc(time_menu)->mw;
1416 if (w != NULL) {
1417 touchwin(w);
1418 wrefresh(w);
1419 }
1420 }
1421 return 0;
1422 }
1423
1424 static int
1425 set_tz_back(menudesc *m, void *arg)
1426 {
1427
1428 zoneinfo_dir[zonerootlen] = 0;
1429 m->cursel = save_cursel;
1430 m->topline = save_topline;
1431 return 0;
1432 }
1433
1434 static int
1435 set_tz_dir(menudesc *m, void *arg)
1436 {
1437
1438 strlcpy(zoneinfo_dir + zonerootlen, m->opts[m->cursel].opt_name,
1439 sizeof zoneinfo_dir - zonerootlen);
1440 save_cursel = m->cursel;
1441 save_topline = m->topline;
1442 m->cursel = 0;
1443 m->topline = 0;
1444 return 0;
1445 }
1446
1447 /*
1448 * Alarm-handler to update example-display
1449 */
1450 static void
1451 /*ARGSUSED*/
1452 timezone_sig(int sig)
1453 {
1454
1455 set_tz_select(NULL, NULL);
1456 alarm(60);
1457 }
1458
1459 static int
1460 tz_sort(const void *a, const void *b)
1461 {
1462 return strcmp(((const menu_ent *)a)->opt_name, ((const menu_ent *)b)->opt_name);
1463 }
1464
1465 static void
1466 tzm_set_names(menudesc *m, void *arg)
1467 {
1468 DIR *dir;
1469 struct dirent *dp;
1470 static int nfiles;
1471 static int maxfiles = 32;
1472 static menu_ent *tz_menu;
1473 static char **tz_names;
1474 void *p;
1475 int maxfname;
1476 char *fp;
1477 struct stat sb;
1478
1479 if (tz_menu == NULL)
1480 tz_menu = calloc(maxfiles, sizeof *tz_menu);
1481 if (tz_names == NULL)
1482 tz_names = malloc(maxfiles * sizeof *tz_names);
1483 if (tz_menu == NULL || tz_names == NULL)
1484 return; /* error - skip timezone setting */
1485 while (nfiles > 0)
1486 free(tz_names[--nfiles]);
1487
1488 dir = opendir(zoneinfo_dir);
1489 fp = strchr(zoneinfo_dir, 0);
1490 if (fp != zoneinfo_dir + zonerootlen) {
1491 tz_names[0] = 0;
1492 tz_menu[0].opt_name = msg_string(MSG_tz_back);
1493 tz_menu[0].opt_action = set_tz_back;
1494 nfiles = 1;
1495 }
1496 maxfname = zoneinfo_dir + sizeof zoneinfo_dir - fp - 1;
1497 if (dir != NULL) {
1498 while ((dp = readdir(dir)) != NULL) {
1499 if (dp->d_namlen > maxfname || dp->d_name[0] == '.')
1500 continue;
1501 strlcpy(fp, dp->d_name, maxfname);
1502 if (stat(zoneinfo_dir, &sb) == -1)
1503 continue;
1504 if (nfiles >= maxfiles) {
1505 p = realloc(tz_menu,
1506 2 * maxfiles * sizeof *tz_menu);
1507 if (p == NULL)
1508 break;
1509 tz_menu = p;
1510 memset(tz_menu + maxfiles, 0,
1511 maxfiles * sizeof *tz_menu);
1512 p = realloc(tz_names,
1513 2 * maxfiles * sizeof *tz_names);
1514 if (p == NULL)
1515 break;
1516 tz_names = p;
1517 memset(tz_names + maxfiles, 0,
1518 maxfiles * sizeof *tz_names);
1519 maxfiles *= 2;
1520 }
1521 if (S_ISREG(sb.st_mode))
1522 tz_menu[nfiles].opt_action = set_tz_select;
1523 else if (S_ISDIR(sb.st_mode)) {
1524 tz_menu[nfiles].opt_action = set_tz_dir;
1525 strlcat(fp, "/",
1526 sizeof(zoneinfo_dir) - (fp - zoneinfo_dir));
1527 } else
1528 continue;
1529 tz_names[nfiles] = strdup(zoneinfo_dir + zonerootlen);
1530 tz_menu[nfiles].opt_name = tz_names[nfiles];
1531 nfiles++;
1532 }
1533 closedir(dir);
1534 }
1535 *fp = 0;
1536
1537 m->opts = tz_menu;
1538 m->numopts = nfiles;
1539 qsort(tz_menu, nfiles, sizeof *tz_menu, tz_sort);
1540 }
1541
1542 void
1543 get_tz_default(void)
1544 {
1545 char localtime_link[STRSIZE];
1546 static char localtime_target[STRSIZE];
1547 int rc;
1548
1549 strlcpy(localtime_link, target_expand("/etc/localtime"),
1550 sizeof localtime_link);
1551
1552 /* Add sanity check that /mnt/usr/share/zoneinfo contains
1553 * something useful
1554 */
1555
1556 rc = readlink(localtime_link, localtime_target,
1557 sizeof(localtime_target) - 1);
1558 if (rc < 0) {
1559 /* error, default to UTC */
1560 tz_default = "UTC";
1561 } else {
1562 localtime_target[rc] = '\0';
1563 tz_default = strchr(strstr(localtime_target, "zoneinfo"), '/') + 1;
1564 }
1565 }
1566
1567 /*
1568 * Choose from the files in usr/share/zoneinfo and set etc/localtime
1569 */
1570 int
1571 set_timezone(void)
1572 {
1573 char localtime_link[STRSIZE];
1574 char localtime_target[STRSIZE];
1575 int menu_no;
1576
1577 strlcpy(zoneinfo_dir, target_expand("/usr/share/zoneinfo/"),
1578 sizeof zoneinfo_dir - 1);
1579 zonerootlen = strlen(zoneinfo_dir);
1580
1581 get_tz_default();
1582
1583 tz_selected = strdup(tz_default);
1584 snprintf(tz_env, sizeof(tz_env), "%s%s", zoneinfo_dir, tz_selected);
1585 setenv("TZ", tz_env, 1);
1586 update_time_display();
1587
1588 signal(SIGALRM, timezone_sig);
1589 alarm(60);
1590
1591 menu_no = new_menu(NULL, NULL, 14, 23, 9,
1592 12, 32, MC_ALWAYS_SCROLL | MC_NOSHORTCUT,
1593 tzm_set_names, NULL, NULL,
1594 "\nPlease consult the install documents.",
1595 MSG_exit_menu_generic);
1596 if (menu_no < 0)
1597 goto done; /* error - skip timezone setting */
1598
1599 time_menu = menu_no;
1600 process_menu(menu_no, NULL);
1601 time_menu = -1;
1602
1603 free_menu(menu_no);
1604
1605 signal(SIGALRM, SIG_IGN);
1606
1607 snprintf(localtime_target, sizeof(localtime_target),
1608 "/usr/share/zoneinfo/%s", tz_selected);
1609 strlcpy(localtime_link, target_expand("/etc/localtime"),
1610 sizeof localtime_link);
1611 unlink(localtime_link);
1612 symlink(localtime_target, localtime_link);
1613
1614 done:
1615 return 1;
1616 }
1617
1618 void
1619 scripting_vfprintf(FILE *f, const char *fmt, va_list ap)
1620 {
1621 va_list ap2;
1622
1623 va_copy(ap2, ap);
1624 if (f)
1625 (void)vfprintf(f, fmt, ap);
1626 if (script)
1627 (void)vfprintf(script, fmt, ap2);
1628 }
1629
1630 void
1631 scripting_fprintf(FILE *f, const char *fmt, ...)
1632 {
1633 va_list ap;
1634
1635 va_start(ap, fmt);
1636 scripting_vfprintf(f, fmt, ap);
1637 va_end(ap);
1638 }
1639
1640 void
1641 add_rc_conf(const char *fmt, ...)
1642 {
1643 FILE *f;
1644 va_list ap;
1645
1646 va_start(ap, fmt);
1647 f = target_fopen("/etc/rc.conf", "a");
1648 if (f != 0) {
1649 scripting_fprintf(NULL, "cat <<EOF >>%s/etc/rc.conf\n",
1650 target_prefix());
1651 scripting_vfprintf(f, fmt, ap);
1652 fclose(f);
1653 scripting_fprintf(NULL, "EOF\n");
1654 }
1655 va_end(ap);
1656 }
1657
1658 int
1659 del_rc_conf(const char *value)
1660 {
1661 FILE *fp, *nfp;
1662 char buf[4096]; /* Ridiculously high, but should be enough in any way */
1663 char *rcconf, *tempname = NULL, *bakname = NULL;
1664 char *cp;
1665 int done = 0;
1666 int fd;
1667 int retval = 0;
1668
1669 /* The paths might seem strange, but using /tmp would require copy instead
1670 * of rename operations. */
1671 if (asprintf(&rcconf, "%s", target_expand("/etc/rc.conf")) < 0
1672 || asprintf(&tempname, "%s", target_expand("/etc/rc.conf.tmp.XXXXXX")) < 0
1673 || asprintf(&bakname, "%s", target_expand("/etc/rc.conf.bak.XXXXXX")) < 0) {
1674 if (rcconf)
1675 free(rcconf);
1676 if (tempname)
1677 free(tempname);
1678 msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
1679 hit_enter_to_continue(NULL, NULL);
1680 return -1;
1681 }
1682
1683 if ((fd = mkstemp(bakname)) < 0) {
1684 msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
1685 hit_enter_to_continue(NULL, NULL);
1686 return -1;
1687 }
1688 close(fd);
1689
1690 if (!(fp = fopen(rcconf, "r+")) || (fd = mkstemp(tempname)) < 0) {
1691 if (fp)
1692 fclose(fp);
1693 msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
1694 hit_enter_to_continue(NULL, NULL);
1695 return -1;
1696 }
1697
1698 nfp = fdopen(fd, "w");
1699 if (!nfp) {
1700 fclose(fp);
1701 close(fd);
1702 msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
1703 hit_enter_to_continue(NULL, NULL);
1704 return -1;
1705 }
1706
1707 while (fgets(buf, sizeof buf, fp) != NULL) {
1708
1709 cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
1710 if (strncmp(cp, value, strlen(value)) == 0) {
1711 cp += strlen(value);
1712 if (*cp != '=')
1713 scripting_fprintf(nfp, "%s", buf);
1714 else
1715 done = 1;
1716 } else {
1717 scripting_fprintf(nfp, "%s", buf);
1718 }
1719 }
1720 fclose(fp);
1721 fclose(nfp);
1722
1723 if (done) {
1724 if (rename(rcconf, bakname)) {
1725 msg_display(MSG_rcconf_backup_failed);
1726 if (!ask_noyes(NULL)) {
1727 retval = -1;
1728 goto done;
1729 }
1730 }
1731
1732 if (rename(tempname, rcconf)) {
1733 if (rename(bakname, rcconf)) {
1734 hit_enter_to_continue(MSG_rcconf_restore_failed,
1735 NULL);
1736 } else {
1737 hit_enter_to_continue(MSG_rcconf_delete_failed,
1738 NULL);
1739 }
1740 } else {
1741 (void)unlink(bakname);
1742 }
1743 }
1744
1745 done:
1746 (void)unlink(tempname);
1747 free(rcconf);
1748 free(tempname);
1749 free(bakname);
1750 return retval;
1751 }
1752
1753 void
1754 add_sysctl_conf(const char *fmt, ...)
1755 {
1756 FILE *f;
1757 va_list ap;
1758
1759 va_start(ap, fmt);
1760 f = target_fopen("/etc/sysctl.conf", "a");
1761 if (f != 0) {
1762 scripting_fprintf(NULL, "cat <<EOF >>%s/etc/sysctl.conf\n",
1763 target_prefix());
1764 scripting_vfprintf(f, fmt, ap);
1765 fclose(f);
1766 scripting_fprintf(NULL, "EOF\n");
1767 }
1768 va_end(ap);
1769 }
1770
1771 void
1772 enable_rc_conf(void)
1773 {
1774
1775 replace("/etc/rc.conf", "s/^rc_configured=NO/rc_configured=YES/");
1776 }
1777
1778 int
1779 check_lfs_progs(void)
1780 {
1781
1782 #ifndef NO_LFS
1783 return binary_available("fsck_lfs") && binary_available("mount_lfs")
1784 && binary_available("newfs_lfs");
1785 #else
1786 return 0;
1787 #endif
1788 }
1789
1790 int
1791 set_is_source(const char *set_name) {
1792 int len = strlen(set_name);
1793 return len >= 3 && memcmp(set_name + len - 3, "src", 3) == 0;
1794 }
1795
1796 const char *
1797 set_dir_for_set(const char *set_name) {
1798 if (strcmp(set_name, "pkgsrc") == 0)
1799 return pkgsrc_dir;
1800 return set_is_source(set_name) ? set_dir_src : set_dir_bin;
1801 }
1802
1803 const char *
1804 ext_dir_for_set(const char *set_name) {
1805 if (strcmp(set_name, "pkgsrc") == 0)
1806 return ext_dir_pkgsrc;
1807 return set_is_source(set_name) ? ext_dir_src : ext_dir_bin;
1808 }
1809
1810 void
1811 do_coloring(unsigned int fg, unsigned int bg) {
1812 if (bg > COLOR_WHITE)
1813 bg = COLOR_BLUE;
1814 if (fg > COLOR_WHITE)
1815 fg = COLOR_WHITE;
1816 if (fg != bg && has_colors()) {
1817 init_pair(1, fg, bg);
1818 wbkgd(stdscr, COLOR_PAIR(1));
1819 wbkgd(mainwin, COLOR_PAIR(1));
1820 wattrset(stdscr, COLOR_PAIR(1));
1821 wattrset(mainwin, COLOR_PAIR(1));
1822 }
1823 /* redraw screen */
1824 touchwin(stdscr);
1825 touchwin(mainwin);
1826 wrefresh(stdscr);
1827 wrefresh(mainwin);
1828 return;
1829 }
1830
1831 int
1832 set_menu_select(menudesc *m, void *arg)
1833 {
1834 *(int *)arg = m->cursel;
1835 return 1;
1836 }
1837
1838 /*
1839 * check wether a binary is available somewhere in PATH,
1840 * return 1 if found, 0 if not.
1841 */
1842 static int
1843 binary_available(const char *prog)
1844 {
1845 char *p, tmp[MAXPATHLEN], *path = getenv("PATH"), *opath;
1846
1847 if (path == NULL)
1848 return access(prog, X_OK) == 0;
1849 path = strdup(path);
1850 if (path == NULL)
1851 return 0;
1852
1853 opath = path;
1854
1855 while ((p = strsep(&path, ":")) != NULL) {
1856 if (strlcpy(tmp, p, MAXPATHLEN) >= MAXPATHLEN)
1857 continue;
1858 if (strlcat(tmp, "/", MAXPATHLEN) >= MAXPATHLEN)
1859 continue;
1860 if (strlcat(tmp, prog, MAXPATHLEN) >= MAXPATHLEN)
1861 continue;
1862 if (access(tmp, X_OK) == 0) {
1863 free(opath);
1864 return 1;
1865 }
1866 }
1867 free(opath);
1868 return 0;
1869 }
1870
1871 const char *
1872 safectime(time_t *t)
1873 {
1874 const char *s = ctime(t);
1875 if (s != NULL)
1876 return s;
1877
1878 // Debugging.
1879 fprintf(stderr, "Can't convert to localtime 0x%jx (%s)\n",
1880 (intmax_t)*t, strerror(errno));
1881 /*123456789012345678901234*/
1882 return "preposterous clock time\n";
1883 }
1884
1885 int
1886 ask_yesno(const char* msgtxt)
1887 {
1888 arg_rv p;
1889
1890 p.arg = __UNCONST(msgtxt);
1891 p.rv = -1;
1892
1893 process_menu(MENU_yesno, &p);
1894 return p.rv;
1895 }
1896
1897 int
1898 ask_noyes(const char *msgtxt)
1899 {
1900 arg_rv p;
1901
1902 p.arg = __UNCONST(msgtxt);
1903 p.rv = -1;
1904
1905 process_menu(MENU_noyes, &p);
1906 return p.rv;
1907 }
1908
1909 int
1910 ask_reedit(const struct disk_partitions *parts)
1911 {
1912 const char *args[2];
1913 arg_rep_int arg;
1914
1915 args[0] = msg_string(parts->pscheme->name);
1916 args[1] = msg_string(parts->pscheme->short_name);
1917 arg.args.argv = args;
1918 arg.args.argc = 2;
1919 arg.rv = 0;
1920 process_menu(MENU_reedit, &arg);
1921
1922 return arg.rv;
1923 }
1924
1925 bool
1926 use_tgz_for_set(const char *set_name)
1927 {
1928 const struct distinfo *dist;
1929
1930 for (dist = dist_list; dist->set != SET_LAST; dist++) {
1931 if (dist->name == NULL)
1932 continue;
1933 if (strcmp(set_name, dist->name) == 0)
1934 return dist->force_tgz;
1935 }
1936
1937 return true;
1938 }
1939
1940 /* Return the postfix used for a given set */
1941 const char *
1942 set_postfix(const char *set_name)
1943 {
1944 return use_tgz_for_set(set_name) ? dist_tgz_postfix : dist_postfix;
1945 }
1946
1947 /*
1948 * Replace positional arguments (encoded as $0 .. $N) in the string
1949 * passed by the contents of the passed argument array.
1950 * Caller must free() the result string.
1951 */
1952 char*
1953 str_arg_subst(const char *src, size_t argc, const char **argv)
1954 {
1955 const char *p, *last;
1956 char *out, *t;
1957 size_t len;
1958
1959 len = strlen(src);
1960 for (p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
1961 char *endp = NULL;
1962 size_t n;
1963 int e;
1964
1965 /* $ followed by a correct numeric position? */
1966 n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
1967 if ((e == 0 || e == ENOTSUP) && n < argc) {
1968 len += strlen(argv[n]);
1969 len -= endp-p;
1970 p = endp-1;
1971 }
1972 }
1973
1974 out = malloc(len+1);
1975 if (out == NULL)
1976 return NULL;
1977
1978 t = out;
1979 for (last = src, p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
1980 char *endp = NULL;
1981 size_t n;
1982 int e;
1983
1984 /* $ followed by a correct numeric position? */
1985 n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
1986 if ((e == 0 || e == ENOTSUP) && n < argc) {
1987 size_t l = p-last;
1988 memcpy(t, last, l);
1989 t += l;
1990 strcpy(t, argv[n]);
1991 t += strlen(argv[n]);
1992 last = endp;
1993 }
1994 }
1995 if (*last) {
1996 strcpy(t, last);
1997 t += strlen(last);
1998 } else {
1999 *t = 0;
2000 }
2001 assert((size_t)(t-out) == len);
2002
2003 return out;
2004 }
2005
2006 /*
2007 * Does this string have any positional args that need expanding?
2008 */
2009 bool
2010 needs_expanding(const char *src, size_t argc)
2011 {
2012 const char *p;
2013
2014 for (p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
2015 char *endp = NULL;
2016 size_t n;
2017 int e;
2018
2019 /* $ followed by a correct numeric position? */
2020 n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
2021 if ((e == 0 || e == ENOTSUP) && n < argc)
2022 return true;
2023 }
2024 return false;
2025 }
2026
2027 /*
2028 * Replace positional arguments (encoded as $0 .. $N) in the
2029 * message by the strings passed as ... and call outfunc
2030 * with the result.
2031 */
2032 static void
2033 msg_display_subst_internal(void (*outfunc)(msg),
2034 const char *master, size_t argc, va_list ap)
2035 {
2036 const char **args, **arg;
2037 char *out;
2038
2039 args = calloc(argc, sizeof(*args));
2040 if (args == NULL)
2041 return;
2042
2043 arg = args;
2044 for (size_t i = 0; i < argc; i++)
2045 *arg++ = va_arg(ap, const char*);
2046
2047 out = str_arg_subst(msg_string(master), argc, args);
2048 if (out != NULL) {
2049 outfunc(out);
2050 free(out);
2051 }
2052 free(args);
2053 }
2054
2055 /*
2056 * Replace positional arguments (encoded as $0 .. $N) in the
2057 * message by the strings passed as ...
2058 */
2059 void
2060 msg_display_subst(const char *master, size_t argc, ...)
2061 {
2062 va_list ap;
2063
2064 va_start(ap, argc);
2065 msg_display_subst_internal(msg_display, master, argc, ap);
2066 va_end(ap);
2067 }
2068
2069 /*
2070 * Same as above, but add to message instead of starting a new one
2071 */
2072 void
2073 msg_display_add_subst(const char *master, size_t argc, ...)
2074 {
2075 va_list ap;
2076
2077 va_start(ap, argc);
2078 msg_display_subst_internal(msg_display_add, master, argc, ap);
2079 va_end(ap);
2080 }
2081
2082 /* initialize have_* variables */
2083 void
2084 check_available_binaries()
2085 {
2086 static int did_test = false;
2087
2088 if (did_test) return;
2089 did_test = 1;
2090
2091 have_raid = binary_available("raidctl");
2092 have_vnd = binary_available("vndconfig");
2093 have_cgd = binary_available("cgdconfig");
2094 have_lvm = binary_available("lvm");
2095 have_gpt = binary_available("gpt");
2096 have_dk = binary_available("dkctl");
2097 }
2098
2099 /*
2100 * Wait for enter and immediately clear the screen after user response
2101 * (in case some longer action follows, so the user has instant feedback)
2102 */
2103 void
2104 hit_enter_to_continue(const char *prompt, const char *title)
2105 {
2106 if (prompt != NULL)
2107 msg_display(prompt);
2108 process_menu(MENU_ok, __UNCONST(title));
2109 msg_clear();
2110 wrefresh(mainwin);
2111 }
2112
2113 /*
2114 * On auto pilot:
2115 * convert an existing set of partitions ot a list of part_usage_info
2116 * so that we "want" exactly what is there already.
2117 */
2118 static bool
2119 usage_info_list_from_parts(struct part_usage_info **list, size_t *count,
2120 struct disk_partitions *parts)
2121 {
2122 struct disk_part_info info;
2123 part_id pno;
2124 size_t no, num;
2125
2126 num = 0;
2127 for (pno = 0; pno < parts->num_part; pno++) {
2128 if (!parts->pscheme->get_part_info(parts, pno, &info))
2129 continue;
2130 num++;
2131 }
2132
2133 *list = calloc(num, sizeof(**list));
2134 if (*list == NULL)
2135 return false;
2136
2137 *count = num;
2138 for (no = pno = 0; pno < parts->num_part && no < num; pno++) {
2139 if (!parts->pscheme->get_part_info(parts, pno, &info))
2140 continue;
2141 (*list)[no].size = info.size;
2142 if (info.last_mounted != NULL && *info.last_mounted != 0) {
2143 strlcpy((*list)[no].mount, info.last_mounted,
2144 sizeof((*list)[no].mount));
2145 (*list)[no].instflags |= PUIINST_MOUNT;
2146 }
2147 (*list)[no].parts = parts;
2148 (*list)[no].cur_part_id = pno;
2149 (*list)[no].cur_start = info.start;
2150 (*list)[no].cur_flags = info.flags;
2151 (*list)[no].type = info.nat_type->generic_ptype;
2152 if ((*list)[no].type == PT_swap) {
2153 (*list)[no].fs_type = FS_SWAP;
2154 (*list)[no].fs_version = 0;
2155 } else {
2156 (*list)[no].fs_type = info.fs_type;
2157 (*list)[no].fs_version = info.fs_sub_type;
2158 }
2159 (*list)[no].fs_opt1 = info.fs_opt1;
2160 (*list)[no].fs_opt2 = info.fs_opt2;
2161 (*list)[no].fs_opt3 = info.fs_opt3;
2162 no++;
2163 }
2164 return true;
2165 }
2166
2167 bool
2168 usage_set_from_parts(struct partition_usage_set *wanted,
2169 struct disk_partitions *parts)
2170 {
2171 memset(wanted, 0, sizeof(*wanted));
2172 wanted->parts = parts;
2173
2174 return usage_info_list_from_parts(&wanted->infos, &wanted->num, parts);
2175 }
2176
2177 struct disk_partitions *
2178 get_inner_parts(struct disk_partitions *parts)
2179 {
2180 daddr_t start, size;
2181 part_id pno;
2182 struct disk_part_info info;
2183
2184 if (parts->pscheme->secondary_scheme == NULL)
2185 return NULL;
2186
2187 start = -1;
2188 size = -1;
2189 if (parts->pscheme->guess_install_target == NULL ||
2190 !parts->pscheme->guess_install_target(parts, &start, &size)) {
2191 for (pno = 0; pno < parts->num_part; pno++) {
2192 if (!parts->pscheme->get_part_info(parts, pno, &info))
2193 continue;
2194 if (!(info.flags & PTI_SEC_CONTAINER))
2195 continue;
2196 start = info.start;
2197 size = info.size;
2198 }
2199 }
2200
2201 if (size > 0)
2202 return parts->pscheme->secondary_partitions(parts, start,
2203 false);
2204
2205 return NULL;
2206 }
2207
2208 bool
2209 install_desc_from_parts(struct install_partition_desc *install,
2210 struct disk_partitions *parts)
2211 {
2212 struct disk_partitions *inner_parts;
2213
2214 memset(install, 0, sizeof(*install));
2215 inner_parts = get_inner_parts(parts);
2216 if (inner_parts != NULL)
2217 parts = inner_parts;
2218
2219 return usage_info_list_from_parts(&install->infos, &install->num,
2220 parts);
2221 }
2222
2223 void
2224 free_usage_set(struct partition_usage_set *wanted)
2225 {
2226 /* XXX - free parts? free clone src? */
2227 free(wanted->write_back);
2228 free(wanted->menu_opts);
2229 free(wanted->infos);
2230 }
2231
2232 void
2233 free_install_desc(struct install_partition_desc *install)
2234 {
2235 size_t i, j;
2236
2237 #ifndef NO_CLONES
2238 for (i = 0; i < install->num; i++) {
2239 struct selected_partitions *src = install->infos[i].clone_src;
2240 if (!(install->infos[i].flags & PUIFLG_CLONE_PARTS) ||
2241 src == NULL)
2242 continue;
2243 free_selected_partitions(src);
2244 for (j = i+1; j < install->num; j++)
2245 if (install->infos[j].clone_src == src)
2246 install->infos[j].clone_src = NULL;
2247 }
2248 #endif
2249
2250 for (i = 0; i < install->num; i++) {
2251 struct disk_partitions * parts = install->infos[i].parts;
2252
2253 if (parts == NULL)
2254 continue;
2255
2256 if (parts->pscheme->free)
2257 parts->pscheme->free(parts);
2258
2259 /* NULL all other references to this parts */
2260 for (j = i+1; j < install->num; j++)
2261 if (install->infos[j].parts == parts)
2262 install->infos[j].parts = NULL;
2263 }
2264
2265 free(install->write_back);
2266 free(install->infos);
2267 }
2268
2269 #ifdef MD_MAY_SWAP_TO
2270 bool
2271 may_swap_if_not_sdmmc(const char *disk)
2272 {
2273 int fd, res;
2274 prop_dictionary_t command_dict, args_dict, results_dict, data_dict;
2275 prop_string_t string;
2276 prop_number_t number;
2277 const char *parent = "";
2278
2279 fd = open(DRVCTLDEV, O_RDONLY, 0);
2280 if (fd == -1)
2281 return true;
2282
2283 command_dict = prop_dictionary_create();
2284 args_dict = prop_dictionary_create();
2285
2286 string = prop_string_create_cstring_nocopy("get-properties");
2287 prop_dictionary_set(command_dict, "drvctl-command", string);
2288 prop_object_release(string);
2289
2290 string = prop_string_create_cstring(disk);
2291 prop_dictionary_set(args_dict, "device-name", string);
2292 prop_object_release(string);
2293
2294 prop_dictionary_set(command_dict, "drvctl-arguments",
2295 args_dict);
2296 prop_object_release(args_dict);
2297
2298 res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
2299 DRVCTLCOMMAND, &results_dict);
2300 prop_object_release(command_dict);
2301 close(fd);
2302 if (res)
2303 return true;
2304
2305 number = prop_dictionary_get(results_dict, "drvctl-error");
2306 if (prop_number_integer_value(number) == 0) {
2307 data_dict = prop_dictionary_get(results_dict,
2308 "drvctl-result-data");
2309 if (data_dict != NULL) {
2310 string = prop_dictionary_get(data_dict,
2311 "device-parent");
2312 if (string != NULL)
2313 parent = prop_string_cstring_nocopy(string);
2314 }
2315 }
2316
2317 prop_object_release(results_dict);
2318
2319 if (parent == NULL)
2320 return true;
2321
2322 return strncmp(parent, "sdmmc", 5) != 0;
2323 }
2324 #endif
2325