forked from kexecboot/kexecboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kexecboot.c
1127 lines (944 loc) · 25.1 KB
/
kexecboot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* kexecboot - A kexec based bootloader
*
* Copyright (c) 2008-2011 Yuri Bushmelev <[email protected]>
* Copyright (c) 2008 Thomas Kunze <[email protected]>
*
* small parts:
* Copyright (c) 2006 Matthew Allum <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input.h>
#include <unistd.h>
#include <sys/mount.h>
#include <ctype.h>
#include <errno.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "util.h"
#include "cfgparser.h"
#include "devicescan.h"
#include "evdevs.h"
#include "menu.h"
#include "kexecboot.h"
#ifdef USE_FBMENU
#include "gui.h"
#endif
#ifdef USE_TEXTUI
#include "tui.h"
#endif
#ifdef USE_ZAURUS
#include "machine/zaurus.h"
#endif
/* Don't re-create devices when executing on host */
#ifdef USE_HOST_DEBUG
#undef USE_DEVICES_RECREATING
#endif
#ifdef USE_MACHINE_KERNEL
/* Machine-dependent kernel patch */
char *machine_kernel = NULL;
#endif
/* NULL-terminated array of kernel search paths
* First item should be filled with machine-dependent path */
char *default_kernels[] = {
#ifdef USE_ZIMAGE
"/mnt/boot/zImage",
"/mnt/zImage",
#endif
#ifdef USE_UIMAGE
"/mnt/boot/uImage",
"/mnt/uImage",
#endif
NULL
};
/* Init mode flag */
int initmode = 0;
/* Contexts available - menu and textview */
typedef enum {
KX_CTX_MENU,
KX_CTX_TEXTVIEW,
} kx_context;
/* Common parameters */
struct params_t {
struct cfgdata_t *cfg;
struct bootconf_t *bootcfg;
kx_menu *menu;
kx_context context;
#ifdef USE_FBMENU
struct gui_t *gui;
#endif
#ifdef USE_TEXTUI
kx_tui *tui;
#endif
};
static char *kxb_ttydev = NULL;
static int kxb_echo_state = 0;
static void atexit_restore_terminal(void)
{
setup_terminal(kxb_ttydev, &kxb_echo_state, 0);
}
#ifdef USE_MACHINE_KERNEL
/* Return lowercased and stripped machine-specific kernel path */
/* Return value should be free()'d */
char *get_machine_kernelpath() {
FILE *f;
char *tmp, *hw = NULL, c;
char line[80];
f = fopen("/proc/cpuinfo", "r");
if (!f) {
perror("/proc/cpuinfo");
exit(-1);
}
/* Search string 'Hardware' */
while (fgets(line, sizeof(line), f)) {
line[strlen(line) - 1] = '\0';
hw = strstr(line, "Hardware");
if (NULL != hw) break;
}
fclose(f);
if ( NULL != hw) {
/* Search colon then skip it and space after */
hw = strchr(hw, ':');
if (NULL == hw) { /* Should not happens but anyway */
log_msg(lg, "Can't find ':' in 'Hardware' line");
return NULL;
}
hw += 2; /* May be ltrim()? */
/* Lowercase name and replace spaces with '_' */
tmp = hw;
while('\0' != *tmp) {
c = *tmp;
if (isspace(c)) {
*tmp = '_';
} else {
*tmp = tolower(c);
}
++tmp;
}
/* Prepend "/mnt/boot/zImage-" to hw */
tmp = malloc(strlen(hw) + 17 + 1); /* strlen("/mnt/boot/zImage-") */
if (NULL == tmp) {
DPRINTF("Can't allocate memory for machine-specific kernel path");
return NULL;
}
strcpy(tmp, "/mnt/boot/zImage-");
strcat(tmp, hw);
return tmp;
}
log_msg(lg, "Can't find 'Hardware' line in cpuinfo");
return NULL;
}
#endif /* USE_MACHINE_KERNEL */
void start_kernel(struct params_t *params, int choice)
{
/* we use var[] instead of *var because sizeof(var) using */
#ifdef USE_HOST_DEBUG
const char kexec_path[] = "/bin/echo";
#else
const char kexec_path[] = KEXEC_PATH;
#endif
const char mount_point[] = MOUNTPOINT;
const char str_cmdline_start[] = "--command-line=root=";
const char str_rootfstype[] = " rootfstype=";
const char str_rootwait[] = " rootwait";
const char str_ubirootdev[] = "ubi0";
const char str_ubimtd[] = " ubi.mtd="; /* max ' ubi.mtd=15' len 11 +1 = 12 */
const char str_hardboot[] = "--load-hardboot";
const char str_memmin[] = "--mem-min=0x8E000000"; //ToDo make this as config option
#ifdef UBI_VID_HDR_OFFSET
const char str_ubimtd_off[] = UBI_VID_HDR_OFFSET;
#else
const char str_ubimtd_off[] = "";
#endif
char mount_dev[16];
char mount_fstype[16];
char str_mtd_id[3];
/* Tags passed from host kernel cmdline to kexec'ed kernel */
const char str_mtdparts[] = " mtdparts=";
const char str_fbcon[] = " fbcon=";
const char str_initrd_start[] = "--initrd=";
/* empty environment */
char *const envp[] = { NULL };
const char *load_argv[] = { NULL, "-l", NULL, NULL, NULL, NULL, NULL , NULL };
const char *exec_argv[] = { NULL, "-e", NULL, NULL};
char *cmdline_arg = NULL, *initrd_arg = NULL;
int n, idx, u;
struct stat sinfo;
struct boot_item_t *item;
item = params->bootcfg->list[choice];
exec_argv[0] = kexec_path;
load_argv[0] = kexec_path;
/* --command-line arg generation */
idx = 2; /* load_argv current option index */
load_argv[idx]=str_hardboot;
load_argv[++idx]=str_memmin;
idx++;
/* fill '--command-line' option */
if (item->device) {
/* default device to mount */
strcpy(mount_dev, item->device);
/* allocate space */
n = sizeof(str_cmdline_start) + strlen(item->device) +
sizeof(str_ubirootdev) + 2 +
sizeof(str_ubimtd) + 2 + sizeof(str_ubimtd_off) + 1 +
sizeof(str_rootwait) +
sizeof(str_rootfstype) + strlen(item->fstype) + 2 +
sizeof(str_mtdparts) + strlenn(params->cfg->mtdparts) +
sizeof(str_fbcon) + strlenn(params->cfg->fbcon) +
sizeof(char) + strlenn(item->cmdline);
cmdline_arg = (char *)malloc(n);
if (NULL == cmdline_arg) {
perror("Can't allocate memory for cmdline_arg");
} else {
strcpy(cmdline_arg, str_cmdline_start); /* --command-line=root= */
if (item->fstype) {
/* default fstype to mount */
strcpy(mount_fstype, item->fstype);
/* extra tags when we detect UBI */
if (!strncmp(item->fstype,"ubi",3)) {
/* mtd id [0-15] - one or two digits */
if(isdigit(atoi(item->device+strlen(item->device)-2))) {
strcpy(str_mtd_id, item->device+strlen(item->device)-2);
strcat(str_mtd_id, item->device+strlen(item->device)-1);
} else {
strcpy(str_mtd_id, item->device+strlen(item->device)-1);
}
/* get corresponding ubi dev to mount */
u = find_attached_ubi_device(str_mtd_id);
sprintf(mount_dev, "/dev/ubi%d", u);
/* FIXME: first volume is hardcoded */
strcat(mount_dev, "_0");
/* HARDCODED: we assume it's ubifs */
strcpy(mount_fstype,"ubifs");
/* extra cmdline tags when we detect ubi */
strcat(cmdline_arg, str_ubirootdev);
/* FIXME: first volume is hardcoded */
strcat(cmdline_arg, "_0");
strcat(cmdline_arg, str_ubimtd);
strcat(cmdline_arg, str_mtd_id);
#ifdef UBI_VID_HDR_OFFSET
strcat(cmdline_arg, ",");
strcat(cmdline_arg, str_ubimtd_off);
#endif
} else {
strcat(cmdline_arg, item->device); /* root=item->device */
}
strcat(cmdline_arg, str_rootfstype);
strcat(cmdline_arg, mount_fstype);
}
strcat(cmdline_arg, str_rootwait);
if (params->cfg->mtdparts) {
strcat(cmdline_arg, str_mtdparts);
strcat(cmdline_arg, params->cfg->mtdparts);
}
if (params->cfg->fbcon) {
strcat(cmdline_arg, str_fbcon);
strcat(cmdline_arg, params->cfg->fbcon);
}
if (item->cmdline) {
strcat(cmdline_arg, "");
strcat(cmdline_arg, item->cmdline);
}
load_argv[idx] = cmdline_arg;
++idx;
}
}
/* fill '--initrd' option */
if (item->initrd) {
/* allocate space */
n = sizeof(str_initrd_start) + strlen(item->initrd);
initrd_arg = (char *)malloc(n);
if (NULL == initrd_arg) {
perror("Can't allocate memory for initrd_arg");
} else {
strcpy(initrd_arg, str_initrd_start); /* --initrd= */
strcat(initrd_arg, item->initrd);
load_argv[idx] = initrd_arg;
++idx;
}
}
/* Append kernelpath as last arg of kexec */
load_argv[idx] = item->kernelpath;
// set harboot options
if (NULL == load_argv[idx]) perror("Can't allocate memory for mem min option");
DPRINTF("load_argv: %s, %s, %s, %s, %s, %s, %s", load_argv[0],
load_argv[1], load_argv[2],
load_argv[3], load_argv[4],
load_argv[5],load_argv[6]);
/* Mount boot device */
if ( -1 == mount(mount_dev, mount_point, mount_fstype,
MS_RDONLY, NULL) ) {
perror("Can't mount boot device");
exit(-1);
}
/* Load kernel */
n = fexecw(kexec_path, (char *const *)load_argv, envp);
if (-1 == n) {
perror("Kexec can't load kernel");
exit(-1);
}
umount(mount_point);
dispose(cmdline_arg);
dispose(initrd_arg);
/* Check /proc/sys/net presence */
if ( -1 == stat("/proc/sys/net", &sinfo) ) {
if (ENOENT == errno) {
/* We have no network, don't issue ifdown() while kexec'ing */
exec_argv[2] = "-x";
DPRINTF("No network is detected, disabling ifdown()");
} else {
perror("Can't stat /proc/sys/net");
}
}
DPRINTF("exec_argv: %s, %s, %s", exec_argv[0],
exec_argv[1], exec_argv[2]);
/* Boot new kernel */
execve(kexec_path, (char *const *)exec_argv, envp);
}
int scan_devices(struct params_t *params)
{
struct charlist *fl;
struct bootconf_t *bootconf;
struct device_t dev;
struct cfgdata_t cfgdata;
int rc,n;
FILE *f;
char mount_dev[16];
char mount_fstype[16];
char str_mtd_id[3];
#ifdef USE_ICONS
kx_cfg_section *sc;
int i;
int rows;
char **xpm_data;
#endif
bootconf = create_bootcfg(4);
if (NULL == bootconf) {
DPRINTF("Can't allocate bootconf structure");
return -1;
}
f = devscan_open(&fl);
if (NULL == f) {
log_msg(lg, "Can't initiate device scan");
return -1;
}
#ifdef USE_ZAURUS
struct zaurus_partinfo_t pinfo;
int zaurus_error = 0;
zaurus_error = zaurus_read_partinfo(&pinfo);
if (0 == zaurus_error) {
/* Fix mtdparts tag */
dispose(params->cfg->mtdparts);
params->cfg->mtdparts = zaurus_mtdparts(&pinfo);
}
#endif
for (;;) {
rc = devscan_next(f, fl, &dev);
if (rc < 0) continue; /* Error */
if (0 == rc) break; /* EOF */
/* initialize with defaults */
strcpy(mount_dev, dev.device);
strcpy(mount_fstype, dev.fstype);
/* We found an ubi erase counter */
if (!strncmp(dev.fstype, "ubi",3)) {
/* attach ubi boot device - mtd id [0-15] */
if(isdigit(atoi(dev.device+strlen(dev.device)-2))) {
strcpy(str_mtd_id, dev.device+strlen(dev.device)-2);
strcat(str_mtd_id, dev.device+strlen(dev.device)-1);
} else {
strcpy(str_mtd_id, dev.device+strlen(dev.device)-1);
}
n = ubi_attach(str_mtd_id);
/* we have attached ubiX and we mount /dev/ubiX_0 */
sprintf(mount_dev, "/dev/ubi%d", n);
/* HARDCODED: first volume */
strcat(mount_dev, "_0");
/* HARDCODED: we assume it's ubifs */
strcpy(mount_fstype, "ubifs");
}
/* Mount device */
if (-1 == mount(mount_dev, MOUNTPOINT, mount_fstype, MS_RDONLY, NULL)) {
log_msg(lg, "+ can't mount device %s: %s", mount_dev, ERRMSG);
goto free_device;
}
/* NOTE: Don't go out before umount'ing */
/* Search boot method and return boot info */
rc = get_bootinfo(&cfgdata);
if (-1 == rc) { /* Error */
goto umount;
}
#ifdef USE_ICONS
/* Iterate over sections found */
if (params->gui) {
for (i = 0; i < cfgdata.count; i++) {
sc = cfgdata.list[i];
if (!sc) continue;
/* Load custom icon */
if (sc->iconpath) {
rows = xpm_load_image(&xpm_data, sc->iconpath);
if (-1 == rows) {
log_msg(lg, "+ can't load xpm icon %s", sc->iconpath);
continue;
}
sc->icondata = xpm_parse_image(xpm_data, rows);
if (!sc->icondata) {
log_msg(lg, "+ can't parse xpm icon %s", sc->iconpath);
continue;
}
xpm_destroy_image(xpm_data, rows);
}
}
}
#endif
umount:
/* Umount device */
if (-1 == umount(MOUNTPOINT)) {
log_msg(lg, "+ can't umount device: %s", ERRMSG);
goto free_cfgdata;
}
if (-1 == rc) { /* Error */
goto free_cfgdata;
}
#ifdef USE_ZAURUS
/* Fix partition sizes. We can have kernel in root and home partitions on NAND */
/* HACK: mtdblock devices are hardcoded */
if (0 == zaurus_error) {
if (0 == strcmp(dev.device, "/dev/mtdblock2")) { /* root */
log_msg(lg, "+ [zaurus root] size of %s will be changed from %llu to %lu",
dev.device, dev.blocks, pinfo.root);
dev.blocks = pinfo.root;
} else if (0 == strcmp(dev.device, "/dev/mtdblock3")) { /* home */
log_msg(lg, "+ [zaurus home] size of %s will be changed from %llu to %lu",
dev.device, dev.blocks, pinfo.home);
dev.blocks = pinfo.home;
}
}
#endif
/* Now we have something in cfgdata */
rc = addto_bootcfg(bootconf, &dev, &cfgdata);
free_cfgdata:
destroy_cfgdata(&cfgdata);
free_device:
free(dev.device);
}
free_charlist(fl);
params->bootcfg = bootconf;
return 0;
}
/* Create system menu */
kx_menu *build_menu(struct params_t *params)
{
kx_menu *menu;
kx_menu_level *ml;
kx_menu_item *mi;
#ifdef USE_ICONS
kx_picture **icons;
if (params->gui) icons = params->gui->icons;
else icons = NULL;
#endif
/* Create menu with 2 levels (main and system) */
menu = menu_create(2);
if (!menu) {
DPRINTF("Can't create menu");
return NULL;
}
/* Create main menu level */
menu->top = menu_level_create(menu, 4, NULL);
/* Create system menu level */
ml = menu_level_create(menu, 6, menu->top);
if (!ml) {
DPRINTF("Can't create system menu");
return menu;
}
mi = menu_item_add(menu->top, A_SUBMENU, "System menu", NULL, ml);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_SYSTEM]);
#endif
mi = menu_item_add(ml, A_PARENTMENU, "Back", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_BACK]);
#endif
mi = menu_item_add(ml, A_RESCAN, "Rescan", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_RESCAN]);
#endif
mi = menu_item_add(ml, A_DEBUG, "Show debug info", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_DEBUG]);
#endif
mi = menu_item_add(ml, A_REBOOT, "Reboot", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_REBOOT]);
#endif
mi = menu_item_add(ml, A_SHUTDOWN, "Shutdown", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_SHUTDOWN]);
#endif
if (!initmode) {
mi = menu_item_add(ml, A_EXIT, "Exit", NULL, NULL);
#ifdef USE_ICONS
if (icons) menu_item_set_data(mi, icons[ICON_EXIT]);
#endif
}
menu->current = menu->top;
menu_item_select(menu, 0);
return menu;
}
/* Fill main menu with boot items */
int fill_menu(struct params_t *params)
{
kx_menu_item *mi;
int i, b_items, max_pri, max_i, *a;
struct boot_item_t *tbi;
struct bootconf_t *bl;
const int sizeof_desc = 160;
char *desc, *label;
#ifdef USE_ICONS
kx_picture *icon;
struct gui_t *gui;
gui = params->gui;
#endif
bl = params->bootcfg;
if ( (NULL != bl) && (bl->fill > 0) ) b_items = bl->fill;
else {
log_msg(lg, "No items for menu found");
return 0;
}
log_msg(lg, "Populating menu: %d item(s)", b_items);
desc = malloc(sizeof_desc);
if (NULL == desc) {
DPRINTF("Can't allocate item description");
goto dirty_exit;
}
a = malloc(b_items * sizeof(*a)); /* Markers array */
if (NULL == a) {
DPRINTF("Can't allocate markers array");
goto dirty_exit;
}
for (i = 0; i < b_items; i++) a[i] = 0; /* Clean markers array */
/* Create menu of sorted by priority boot items */
max_i = -1;
for(;;) {
max_pri = -1;
/* Search item with maximum priority */
for (i = 0; i < b_items; i++) {
if (0 == a[i]) { /* Check that item is not processed yet */
tbi = bl->list[i];
if (tbi->priority > max_pri) {
max_pri = tbi->priority; /* Max priority */
max_i = i; /* Max priority item index */
}
}
}
if (max_pri >= 0) {
a[max_i] = 1; /* Mark item as processed */
/* We have found new max priority - insert into menu */
tbi = bl->list[max_i];
snprintf(desc, sizeof_desc, "%s %s %lluMb",
tbi->device, tbi->fstype, tbi->blocks/1024);
if (tbi->label)
label = tbi->label;
else
label = tbi->kernelpath + sizeof(MOUNTPOINT) - 1;
log_msg(lg, "+ [%s]", label);
mi = menu_item_add(params->menu->top, A_DEVICES + max_i,
label, desc, NULL);
#ifdef USE_ICONS
if (gui) {
/* Search associated with boot item icon if any */
icon = tbi->icondata;
if (!icon && (gui->icons)) {
/* We have no custom icon - use default */
switch (tbi->dtype) {
case DVT_STORAGE:
icon = gui->icons[ICON_STORAGE];
break;
case DVT_MMC:
icon = gui->icons[ICON_MMC];
break;
case DVT_MTD:
icon = gui->icons[ICON_MEMORY];
break;
case DVT_UNKNOWN:
default:
break;
}
}
/* Add icon to menu */
if (mi) mi->data = icon;
}
#endif
}
if (-1 == max_pri) break; /* We have no items to process */
}
free(a);
free(desc);
return 0;
dirty_exit:
dispose(desc);
return -1;
}
/* Return 0 if we are ordinary app or 1 if we are init */
int do_init(void)
{
/* When our pid is 1 we are init-process */
if ( 1 != getpid() ) {
return 0;
}
log_msg(lg, "I'm the init-process!");
#ifdef USE_DEVTMPFS
if (-1 == mount("devtmpfs", "/dev", "devtmpfs",
0, NULL) ) {
perror("Can't mount devtmpfs");
}
#endif
/* Mount procfs */
if ( -1 == mount("proc", "/proc", "proc",
0, NULL) ) {
perror("Can't mount procfs");
exit(-1);
}
/* Mount sysfs */
if ( -1 == mount("sysfs", "/sys", "sysfs",
0, NULL) ) {
perror("Can't mount sysfs");
exit(-1);
}
FILE *f;
/* Set up console loglevel */
f = fopen("/proc/sys/kernel/printk", "w");
if (NULL == f) {
/* CONFIG_PRINTK may be disabled */
log_msg(lg, "/proc/sys/kernel/printk", ERRMSG);
} else {
fputs("0 4 1 7\n", f);
fclose(f);
}
return 1;
}
int do_rescan(struct params_t *params)
{
int i;
/* Clean top menu level except system menu item */
/* FIXME should be done by some function from menu module */
kx_menu_item *mi;
for (i = 1; i < params->menu->top->count; i++) {
mi = params->menu->top->list[i];
if (mi) {
dispose(mi->label);
dispose(mi->description);
free(mi);
}
params->menu->top->list[i] = NULL;
}
params->menu->top->count = 1;
#ifdef USE_ICONS
/* Destroy icons */
/* FIXME should be done by some function from devicescan module */
for (i = 0; i < params->bootcfg->fill; i++) {
fb_destroy_picture(params->bootcfg->list[i]->icondata);
}
#endif
free_bootcfg(params->bootcfg);
params->bootcfg = NULL;
scan_devices(params);
return fill_menu(params);
}
/* Process menu context
* Return 0 to select, <0 to raise error, >0 to continue
*/
int process_ctx_menu(struct params_t *params, int action) {
static int rc;
static int menu_action;
static kx_menu *menu;
menu = params->menu;
#ifdef USE_NUMKEYS
/* Some hacks to allow menu items selection by keys 0-9 */
if ((action >= A_KEY0) && (action <= A_KEY9)) {
rc = action - A_KEY0;
if (-1 == menu_item_select_by_no(menu, rc)) {
/* There is no item with such number - do nothing */
return 1;
} else {
action = A_SELECT;
}
}
#endif
menu_action = (A_SELECT == action ? menu->current->current->id : action);
rc = 1;
switch (menu_action) {
case A_UP:
menu_item_select(menu, -1);
break;
case A_DOWN:
menu_item_select(menu, 1);
break;
case A_SUBMENU:
menu->current = menu->current->current->submenu;
break;
case A_PARENTMENU:
menu->current = menu->current->parent;
break;
case A_REBOOT:
#ifdef USE_FBMENU
gui_show_msg(params->gui, "Rebooting...");
#endif
#ifdef USE_TEXTUI
tui_show_msg(params->tui, "Rebooting...");
#endif
#ifdef USE_HOST_DEBUG
sleep(1);
#else
sync();
/* if ( -1 == reboot(LINUX_REBOOT_CMD_RESTART) ) { */
if ( -1 == reboot(RB_AUTOBOOT) ) {
log_msg(lg, "Can't initiate reboot: %s", ERRMSG);
}
#endif
break;
case A_SHUTDOWN:
#ifdef USE_FBMENU
gui_show_msg(params->gui, "Shutting down...");
#endif
#ifdef USE_TEXTUI
tui_show_msg(params->tui, "Shutting down...");
#endif
#ifdef USE_HOST_DEBUG
sleep(1);
#else
sync();
/* if ( -1 == reboot(LINUX_REBOOT_CMD_POWER_OFF) ) { */
if ( -1 == reboot(RB_POWER_OFF) ) {
log_msg(lg, "Can't initiate shutdown: %s", ERRMSG);
}
#endif
break;
case A_RESCAN:
#ifdef USE_FBMENU
gui_show_msg(params->gui, "Rescanning devices.\nPlease wait...");
#endif
#ifdef USE_TEXTUI
tui_show_msg(params->tui, "Rescanning devices.\nPlease wait...");
#endif
if (-1 == do_rescan(params)) {
log_msg(lg, "Rescan failed");
return -1;
}
menu = params->menu;
break;
case A_DEBUG:
params->context = KX_CTX_TEXTVIEW;
break;
case A_EXIT:
if (initmode) break; // don't exit if we are init
case A_ERROR:
rc = -1;
break;
#ifdef USE_TIMEOUT
case A_TIMEOUT: // timeout was reached - boot 1st kernel if exists
if (menu->current->count > 1) {
menu_item_select(menu, 0); /* choose first item */
menu_item_select(menu, 1); /* and switch to next item */
rc = 0;
}
break;
#endif
default:
if (menu_action >= A_DEVICES) rc = 0;
break;
}
return rc;
}
/* Draw menu context */
void draw_ctx_menu(struct params_t *params)
{
#ifdef USE_FBMENU
gui_show_menu(params->gui, params->menu);
#endif
#ifdef USE_TEXTUI
tui_show_menu(params->tui, params->menu);
#endif
}
/* Process text view context
* Return 0 to select, <0 to raise error, >0 to continue
*/
int process_ctx_textview(struct params_t *params, int action) {
static int rc;
rc = 1;
switch (action) {
case A_UP:
if (lg->current_line_no > 0) --lg->current_line_no;
break;
case A_DOWN:
if (lg->current_line_no + 1 < lg->rows->fill) ++lg->current_line_no;
break;
case A_SELECT:
/* Rewind log view to top. This should make log view usable
* on devices with 2 buttons only (DOWN and SELECT)
*/
lg->current_line_no = 0;
params->context = KX_CTX_MENU;
break;
case A_EXIT:
if (initmode) break; // don't exit if we are init
case A_ERROR:
rc = -1;
break;
}
return rc;
}
/* Draw text view context */
void draw_ctx_textview(struct params_t *params)
{
#ifdef USE_FBMENU
gui_show_text(params->gui, lg);
#endif
#ifdef USE_TEXTUI
tui_show_text(params->tui, lg);
#endif
}
/* Main event loop */
int do_main_loop(struct params_t *params, kx_inputs *inputs)
{
int rc = 0;
int action;
/* Start with menu context */
params->context = KX_CTX_MENU;
draw_ctx_menu(params);
/* Event loop */
do {
/* Read events */
action = inputs_process(inputs);
if (action != A_NONE) {
/* Process events in current context */
switch (params->context) {
case KX_CTX_MENU:
rc = process_ctx_menu(params, action);
break;
case KX_CTX_TEXTVIEW:
rc = process_ctx_textview(params, action);
}
/* Draw current context */
if (rc > 0) {
switch (params->context) {
case KX_CTX_MENU:
draw_ctx_menu(params);
break;
case KX_CTX_TEXTVIEW: