]> git.ozlabs.org Git - petitboot/blob - ui/common/ui-system.c
Remove ked ctrl-B hot key
[petitboot] / ui / common / ui-system.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 #include "log/log.h"
30 #include <system/system.h>
31 #include "talloc/talloc.h"
32 #include "loader.h"
33 #include "ui-system.h"
34
35 /**
36  * run_kexec_local - Final kexec helper.
37  * @l_image: The local image file for kexec to execute.
38  * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL.
39  * @args: Optional command line args for kexec --append, can be NULL.
40  */
41
42 static int run_kexec_local(const char *l_image, const char *l_initrd,
43         const char *args)
44 {
45         int result;
46         const char *argv[6];
47         const char **p;
48         char *s_initrd = NULL;
49         char *s_args = NULL;
50
51         p = argv;
52         *p++ = pb_system_apps.kexec;            /* 1 */
53
54         if (l_initrd) {
55                 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
56                 assert(s_initrd);
57                 *p++ = s_initrd;                 /* 2 */
58         }
59
60         if (args) {
61                 s_args = talloc_asprintf(NULL, "--append=%s", args);
62                 assert(s_args);
63                 *p++ = s_args;                   /* 3 */
64         }
65
66         /* First try by telling kexec to run shutdown */
67
68         *(p + 0) = l_image;
69         *(p + 1) = NULL;
70
71         result = pb_run_cmd(argv);
72
73         /* kexec will return zero on success */
74         /* On error, force a kexec with the -f option */
75
76         if (result) {
77                 *(p + 0) = "-f";                /* 4 */
78                 *(p + 1) = l_image;             /* 5 */
79                 *(p + 2) = NULL;                /* 6 */
80
81                 result = pb_run_cmd(argv);
82         }
83
84         if (result)
85                 pb_log("%s: failed: (%d)\n", __func__, result);
86
87         talloc_free(s_initrd);
88         talloc_free(s_args);
89
90         return result;
91 }
92
93 /**
94  * pb_run_kexec - Run kexec with the supplied boot options.
95  */
96
97 int pb_run_kexec(const struct pb_kexec_data *kd)
98 {
99         int result;
100         char *l_image = NULL;
101         char *l_initrd = NULL;
102
103         pb_log("%s: image:  '%s'\n", __func__, kd->image);
104         pb_log("%s: initrd: '%s'\n", __func__, kd->initrd);
105         pb_log("%s: args:   '%s'\n", __func__, kd->args);
106
107         if (kd->image) {
108                 l_image = pb_load_file(NULL, kd->image);
109                 if (!l_image)
110                         return -1;
111         }
112
113         if (kd->initrd) {
114                 l_initrd = pb_load_file(NULL, kd->initrd);
115                 if (!l_initrd)
116                         return -1;
117         }
118
119         if (!l_image && !l_initrd)
120                 return -1;
121
122         result = run_kexec_local(l_image, l_initrd, kd->args);
123
124         talloc_free(l_image);
125         talloc_free(l_initrd);
126
127         return result;
128 }
129
130 /**
131  * pb_elf_hash - Standard elf hash routine.
132  */
133
134 unsigned int pb_elf_hash(const char *str)
135 {
136         unsigned int h = 0, g;
137
138         while (*str) {
139                 h = (h << 4) + *str++;
140                 g = h & 0xf0000000;
141                 if (g)
142                         h ^= g >> 24;
143                 h &= ~g;
144         }
145         pb_log("%s: %x\n", __func__, h);
146         return h;
147 }
148
149 /**
150  * pb_cat_hash - Hashes concatenation of two strings.
151  */
152
153 unsigned int pb_cat_hash(const char *a, const char *b)
154 {
155         unsigned int h;
156         char *s;
157
158         s = talloc_asprintf(NULL, "%s%s", a, b);
159         h = pb_elf_hash(s);
160         talloc_free(s);
161
162         return h;
163 }