diff options
Diffstat (limited to 'utils/nwztools/plattools/dualboot.c')
-rw-r--r-- | utils/nwztools/plattools/dualboot.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/utils/nwztools/plattools/dualboot.c b/utils/nwztools/plattools/dualboot.c new file mode 100644 index 0000000000..965fabed72 --- /dev/null +++ b/utils/nwztools/plattools/dualboot.c | |||
@@ -0,0 +1,181 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2011 by Amaury Pouly | ||
11 | * | ||
12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing | ||
13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or | ||
16 | * modify it under the terms of the GNU General Public License | ||
17 | * as published by the Free Software Foundation; either version 2 | ||
18 | * of the License, or (at your option) any later version. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | #include "nwz_lib.h" | ||
25 | #include "nwz_plattools.h" | ||
26 | #include <time.h> | ||
27 | |||
28 | /* Important Note: this bootloader is carefully written so that in case of | ||
29 | * error, the OF is run. This seems like the safest option since the OF is | ||
30 | * always there and might do magic things. */ | ||
31 | |||
32 | bool boot_rockbox(void) | ||
33 | { | ||
34 | /* get time */ | ||
35 | struct timeval deadline; | ||
36 | if(gettimeofday(&deadline, NULL) != 0) | ||
37 | { | ||
38 | nwz_lcdmsg(false, 0, 2, "Cannot get time"); | ||
39 | sleep(2); | ||
40 | return false; | ||
41 | } | ||
42 | /* open input device */ | ||
43 | int input_fd = nwz_key_open(); | ||
44 | if(input_fd < 0) | ||
45 | { | ||
46 | nwz_lcdmsg(false, 0, 2, "Cannot open input device"); | ||
47 | sleep(2); | ||
48 | return false; | ||
49 | } | ||
50 | deadline.tv_sec += 5; | ||
51 | /* wait for user action */ | ||
52 | bool boot_rb = false; | ||
53 | while(true) | ||
54 | { | ||
55 | /* get time */ | ||
56 | struct timeval cur_time; | ||
57 | if(gettimeofday(&cur_time, NULL) != 0) | ||
58 | { | ||
59 | nwz_lcdmsg(false, 0, 4, "Cannot get time"); | ||
60 | sleep(2); | ||
61 | break; | ||
62 | } | ||
63 | /* check timeout */ | ||
64 | if(cur_time.tv_sec > deadline.tv_sec) | ||
65 | break; | ||
66 | if(cur_time.tv_sec == deadline.tv_sec && cur_time.tv_usec >= deadline.tv_usec) | ||
67 | break; | ||
68 | /* print message */ | ||
69 | int sec_left = deadline.tv_sec - cur_time.tv_sec; | ||
70 | sec_left += (deadline.tv_usec - cur_time.tv_usec + 999999) / 1000000; /* round up */ | ||
71 | nwz_lcdmsgf(false, 0, 2, "Booting OF in %d seconds ", sec_left); | ||
72 | nwz_lcdmsg(false, 0, 3, "Press BACK to boot RB"); | ||
73 | /* wait for a key (1s) */ | ||
74 | int ret = nwz_key_wait_event(input_fd, 1000000); | ||
75 | if(ret != 1) | ||
76 | continue; | ||
77 | struct input_event evt; | ||
78 | if(nwz_key_read_event(input_fd, &evt) != 1) | ||
79 | continue; | ||
80 | if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_BACK && !nwz_key_event_is_press(&evt)) | ||
81 | { | ||
82 | boot_rb = true; | ||
83 | break; | ||
84 | } | ||
85 | } | ||
86 | nwz_key_close(input_fd); | ||
87 | return boot_rb; | ||
88 | } | ||
89 | |||
90 | static char *boot_rb_argv[] = | ||
91 | { | ||
92 | "lcdmsg", | ||
93 | "-c", | ||
94 | "-l", | ||
95 | "0,3", | ||
96 | "Hello from RB", | ||
97 | NULL | ||
98 | }; | ||
99 | |||
100 | static void wait_key(void) | ||
101 | { | ||
102 | int input_fd = nwz_key_open(); | ||
103 | /* display input state in a loop */ | ||
104 | while(1) | ||
105 | { | ||
106 | /* wait for event (10ms) */ | ||
107 | int ret = nwz_key_wait_event(input_fd, 10000); | ||
108 | if(ret != 1) | ||
109 | continue; | ||
110 | struct input_event evt; | ||
111 | if(nwz_key_read_event(input_fd, &evt) != 1) | ||
112 | continue; | ||
113 | if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_BACK && !nwz_key_event_is_press(&evt)) | ||
114 | break; | ||
115 | } | ||
116 | /* finish nicely */ | ||
117 | nwz_key_close(input_fd); | ||
118 | } | ||
119 | |||
120 | int NWZ_TOOL_MAIN(all_tools)(int argc, char **argv); | ||
121 | |||
122 | int main(int argc, char **argv) | ||
123 | { | ||
124 | #if 0 | ||
125 | nwz_lcdmsg(true, 0, 0, "dualboot"); | ||
126 | if(boot_rockbox()) | ||
127 | { | ||
128 | /* boot rockox */ | ||
129 | nwz_lcdmsg(true, 0, 3, "Booting rockbox..."); | ||
130 | execvp("/usr/local/bin/lcdmsg", boot_rb_argv); | ||
131 | nwz_lcdmsg(false, 0, 4, "failed."); | ||
132 | sleep(5); | ||
133 | } | ||
134 | /* if for some reason, running rockbox failed, then try to run the OF */ | ||
135 | /* boot OF */ | ||
136 | nwz_lcdmsg(true, 0, 3, "Booting OF..."); | ||
137 | execvp("/usr/local/bin/SpiderApp.of", argv); | ||
138 | nwz_lcdmsg(false, 0, 4, "failed."); | ||
139 | sleep(5); | ||
140 | /* if we reach this point, everything failed, so return an error so that | ||
141 | * sysmgrd knows something is wrong */ | ||
142 | return 1; | ||
143 | #elif 0 | ||
144 | const char *args_mount[] = {"mount", NULL}; | ||
145 | int status; | ||
146 | char *output = nwz_run_pipe("mount", args_mount, &status); | ||
147 | nwz_lcdmsgf(true, 0, 0, "%d\n%s", status, output); | ||
148 | free(output); | ||
149 | wait_key(); | ||
150 | const char *args_ls[] = {"ls", "/var", NULL}; | ||
151 | output = nwz_run_pipe("ls", args_ls, &status); | ||
152 | nwz_lcdmsgf(true, 0, 0, "%d\n%s", status, output); | ||
153 | free(output); | ||
154 | wait_key(); | ||
155 | const char *args_glogctl[] = {"glogctl", "flush", NULL}; | ||
156 | output = nwz_run_pipe("/usr/local/bin/glogctl", args_glogctl, &status); | ||
157 | nwz_lcdmsgf(true, 0, 0, "%d\n%s", status, output); | ||
158 | free(output); | ||
159 | wait_key(); | ||
160 | system("cp /var/GEMINILOG* /contents/"); | ||
161 | sync(); | ||
162 | execvp("/usr/local/bin/SpiderApp.of", argv); | ||
163 | return 0; | ||
164 | #else | ||
165 | /* make sure backlight is on */ | ||
166 | int fb_fd = nwz_fb_open(true); | ||
167 | if(fb_fd >= 0) | ||
168 | { | ||
169 | struct nwz_fb_brightness bl; | ||
170 | nwz_fb_get_brightness(fb_fd, &bl); | ||
171 | bl.level = NWZ_FB_BL_MAX_LEVEL; | ||
172 | nwz_fb_set_brightness(fb_fd, &bl); | ||
173 | nwz_fb_close(fb_fd); | ||
174 | } | ||
175 | /* run all tools menu */ | ||
176 | NWZ_TOOL_MAIN(all_tools)(argc, argv); | ||
177 | /* run OF */ | ||
178 | execvp("/usr/local/bin/SpiderApp.of", argv); | ||
179 | return 0; | ||
180 | #endif | ||
181 | } | ||