47cef66c5643d84ac63463c947451d5bccff169a
[profile/ivi/syslinux.git] / com32 / hdt / hdt-cli-hdt.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2009 Pierre-Alexandre Meyer - All Rights Reserved
4  *
5  *   Permission is hereby granted, free of charge, to any person
6  *   obtaining a copy of this software and associated documentation
7  *   files (the "Software"), to deal in the Software without
8  *   restriction, including without limitation the rights to use,
9  *   copy, modify, merge, publish, distribute, sublicense, and/or
10  *   sell copies of the Software, and to permit persons to whom
11  *   the Software is furnished to do so, subject to the following
12  *   conditions:
13  *
14  *   The above copyright notice and this permission notice shall
15  *   be included in all copies or substantial portions of the Software.
16  *
17  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  *   OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * -----------------------------------------------------------------------
27  */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <syslinux/config.h>
33
34 #include "hdt-menu.h"
35 #include "hdt-cli.h"
36 #include "hdt-common.h"
37
38 /**
39  * cli_clear_screen - clear (erase) the entire screen
40  **/
41 static void cli_clear_screen(int argc __unused, char **argv __unused,
42                              struct s_hardware *hardware __unused)
43 {
44     clear_screen();
45 }
46
47 /**
48  * main_show_modes - show availables modes
49  **/
50 static void main_show_modes(int argc __unused, char **argv __unused,
51                             struct s_hardware *hardware __unused)
52 {
53     int i = 0;
54
55     reset_more_printf();
56     printf("Available modes:\n");
57     while (list_modes[i]) {
58         printf("%s ", list_modes[i]->name);
59         i++;
60     }
61     printf("\n");
62 }
63
64 /**
65  * cli_set_mode - set the mode of the cli, in the cli
66  *
67  * The mode number must be supplied in argv, position 0.
68  **/
69 static void cli_set_mode(int argc, char **argv, struct s_hardware *hardware)
70 {
71     cli_mode_t new_mode;
72
73     reset_more_printf();
74     if (argc <= 0) {
75         more_printf("Which mode?\n");
76         return;
77     }
78
79     /*
80      * Note! argv[0] is a string representing the mode, we need the
81      * equivalent cli_mode_t to pass it to set_mode.
82      */
83     new_mode = mode_s_to_mode_t(argv[0]);
84     set_mode(new_mode, hardware);
85 }
86
87 /**
88  * do_exit - shared helper to exit a mode
89  **/
90 static void do_exit(int argc __unused, char **argv __unused,
91                     struct s_hardware *hardware)
92 {
93     int new_mode = HDT_MODE;
94
95     switch (hdt_cli.mode) {
96     case HDT_MODE:
97         new_mode = EXIT_MODE;
98         break;
99     default:
100         new_mode = HDT_MODE;
101         break;
102     }
103
104     dprintf("CLI DEBUG: Switching from mode %d to mode %d\n", hdt_cli.mode,
105             new_mode);
106     set_mode(new_mode, hardware);
107 }
108
109 /**
110  * show_cli_help - shared helper to show available commands
111  **/
112 static void show_cli_help(int argc __unused, char **argv __unused,
113                           struct s_hardware *hardware __unused)
114 {
115     int j = 0;
116     struct cli_mode_descr *current_mode;
117     struct cli_callback_descr *associated_module = NULL;
118
119     find_cli_mode_descr(hdt_cli.mode, &current_mode);
120
121     printf("Available commands are:\n");
122
123     /* List first default modules of the mode */
124     if (current_mode->default_modules && current_mode->default_modules->modules) {
125         while (current_mode->default_modules->modules[j].name) {
126             printf("%s ", current_mode->default_modules->modules[j].name);
127             j++;
128         }
129         printf("\n");
130     }
131
132     /* List secondly the show modules of the mode */
133     if (current_mode->show_modules && current_mode->show_modules->modules) {
134         printf("\nshow commands:\n");
135         j = 0;
136         while (current_mode->show_modules->modules[j].name) {
137             printf("%s ", current_mode->show_modules->modules[j].name);
138             j++;
139         }
140         printf("\n");
141     }
142
143     /* List thirdly the set modules of the mode */
144     if (current_mode->set_modules && current_mode->set_modules->modules) {
145         printf("\nset commands:\n");
146         j = 0;
147         while (current_mode->set_modules->modules[j].name) {
148             printf("%s ", current_mode->set_modules->modules[j].name);
149             j++;
150         }
151         printf("\n");
152     }
153
154     /* List finally the default modules of the hdt mode */
155     if (current_mode->mode != hdt_mode.mode &&
156         hdt_mode.default_modules && hdt_mode.default_modules->modules) {
157         j = 0;
158         while (hdt_mode.default_modules->modules[j].name) {
159             /*
160              * Any default command that is present in hdt mode but
161              * not in the current mode is available. A default
162              * command can be redefined in the current mode though.
163              * This next call test this use case: if it is
164              * overwritten, do not print it again.
165              */
166             find_cli_callback_descr(hdt_mode.default_modules->modules[j].name,
167                                     current_mode->default_modules,
168                                     &associated_module);
169             if (associated_module == NULL)
170                 printf("%s ", hdt_mode.default_modules->modules[j].name);
171             j++;
172         }
173         printf("\n");
174     }
175
176     printf("\n");
177     main_show_modes(argc, argv, hardware);
178 }
179
180 /**
181  * show_cli_help - shared helper to show available commands
182  **/
183 static void goto_menu(int argc __unused, char **argv __unused,
184                       struct s_hardware *hardware)
185 {
186     char version_string[256];
187     snprintf(version_string, sizeof version_string, "%s %s (%s)",
188              PRODUCT_NAME, VERSION, CODENAME);
189     start_menu_mode(hardware, version_string);
190     return;
191 }
192
193 /**
194  * main_show_summary - give an overview of the system
195  **/
196 void main_show_summary(int argc __unused, char **argv __unused,
197                        struct s_hardware *hardware)
198 {
199     reset_more_printf();
200     clear_screen();
201     main_show_cpu(argc, argv, hardware);
202     if (hardware->is_dmi_valid) {
203         more_printf("System\n");
204         more_printf(" Manufacturer : %s\n", hardware->dmi.system.manufacturer);
205         more_printf(" Product Name : %s\n", hardware->dmi.system.product_name);
206         more_printf(" Serial       : %s\n", hardware->dmi.system.serial);
207         more_printf("Bios\n");
208         more_printf(" Version      : %s\n", hardware->dmi.bios.version);
209         more_printf(" Release      : %s\n", hardware->dmi.bios.release_date);
210         more_printf("Memory Size   : %lu MB (%lu KB)\n",
211                     (hardware->detected_memory_size + (1 << 9)) >> 10,
212                     hardware->detected_memory_size);
213     }
214     main_show_pci(argc, argv, hardware);
215
216     if (hardware->is_pxe_valid)
217         main_show_pxe(argc, argv, hardware);
218
219     main_show_kernel(argc, argv, hardware);
220 }
221
222 void main_show_hdt(int argc __unused, char **argv __unused,
223                    struct s_hardware *hardware __unused)
224 {
225     reset_more_printf();
226     more_printf("HDT\n");
227     more_printf(" Product        : %s\n", PRODUCT_NAME);
228     more_printf(" Version        : %s (%s)\n", VERSION, CODENAME);
229     more_printf(" Website        : %s\n", WEBSITE_URL);
230     more_printf(" Mailing List   : %s\n", CONTACT);
231     more_printf(" Project Leader : %s\n", AUTHOR);
232     more_printf(" Core Developer : %s\n", CORE_DEVELOPER);
233     char *contributors[NB_CONTRIBUTORS] = CONTRIBUTORS;
234     for (int c = 0; c < NB_CONTRIBUTORS; c++) {
235         more_printf(" Contributor    : %s\n", contributors[c]);
236     }
237 }
238
239 /**
240  * do_reboot - reboot the system
241  **/
242 static void do_reboot(int argc __unused, char **argv __unused,
243                       struct s_hardware *hardware)
244 {
245     /* Use specific syslinux call if needed */
246     if (issyslinux())
247         return runsyslinuxcmd(hardware->reboot_label);
248     else
249         return csprint(hardware->reboot_label, 0x07);
250 }
251
252 /* Default hdt mode */
253 struct cli_callback_descr list_hdt_default_modules[] = {
254     {
255      .name = CLI_CLEAR,
256      .exec = cli_clear_screen,
257      },
258     {
259      .name = CLI_EXIT,
260      .exec = do_exit,
261      },
262     {
263      .name = CLI_HELP,
264      .exec = show_cli_help,
265      },
266     {
267      .name = CLI_MENU,
268      .exec = goto_menu,
269      },
270     {
271      .name = CLI_REBOOT,
272      .exec = do_reboot,
273      },
274     {
275      .name = CLI_HISTORY,
276      .exec = print_history,
277      },
278     {
279      .name = NULL,
280      .exec = NULL},
281 };
282
283 struct cli_callback_descr list_hdt_show_modules[] = {
284     {
285      .name = CLI_SUMMARY,
286      .exec = main_show_summary,
287      },
288     {
289      .name = CLI_PCI,
290      .exec = main_show_pci,
291      },
292     {
293      .name = CLI_DMI,
294      .exec = main_show_dmi,
295      },
296     {
297      .name = CLI_CPU,
298      .exec = main_show_cpu,
299      },
300     {
301      .name = CLI_DISK,
302      .exec = disks_summary,
303      },
304     {
305      .name = CLI_PXE,
306      .exec = main_show_pxe,
307      },
308     {
309      .name = CLI_SYSLINUX,
310      .exec = main_show_syslinux,
311      },
312     {
313      .name = CLI_KERNEL,
314      .exec = main_show_kernel,
315      },
316     {
317      .name = CLI_VESA,
318      .exec = main_show_vesa,
319      },
320     {
321      .name = CLI_HDT,
322      .exec = main_show_hdt,
323      },
324     {
325      .name = CLI_VPD,
326      .exec = main_show_vpd,
327      },
328     {
329      .name = CLI_MEMORY,
330      .exec = show_dmi_memory_modules,
331      },
332     {
333      .name = CLI_ACPI,
334      .exec = main_show_acpi,
335      },
336     {
337      .name = "modes",
338      .exec = main_show_modes,
339      },
340     {
341      .name = NULL,
342      .exec = NULL,
343      },
344 };
345
346 struct cli_callback_descr list_hdt_set_modules[] = {
347     {
348      .name = CLI_MODE,
349      .exec = cli_set_mode,
350      },
351     {
352      .name = NULL,
353      .exec = NULL,
354      },
355 };
356
357 struct cli_module_descr hdt_default_modules = {
358     .modules = list_hdt_default_modules,
359 };
360
361 struct cli_module_descr hdt_show_modules = {
362     .modules = list_hdt_show_modules,
363     .default_callback = main_show_summary,
364 };
365
366 struct cli_module_descr hdt_set_modules = {
367     .modules = list_hdt_set_modules,
368 };
369
370 struct cli_mode_descr hdt_mode = {
371     .mode = HDT_MODE,
372     .name = CLI_HDT,
373     .default_modules = &hdt_default_modules,
374     .show_modules = &hdt_show_modules,
375     .set_modules = &hdt_set_modules,
376 };