]> git.ozlabs.org Git - petitboot/blob - ui/common/ui-system.c
3f54191bfd5671c4e92476353a30401a8741bab3
[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[8];
47         const char **p;
48
49         p = argv;
50         *p++ = pb_system_apps.kexec;            /* 1 */
51
52         if (l_initrd) {
53                 *p++ = "--initrd";              /* 2 */
54                 *p++ = l_initrd;                /* 3 */
55         }
56
57         if (args) {
58                 *p++ = "--append";              /* 4 */
59                 *p++ = args;                    /* 5 */
60         }
61
62         /* First try by telling kexec to run shutdown */
63
64         *(p + 0) = l_image;
65         *(p + 1) = NULL;
66
67         result = pb_run_cmd(argv);
68
69         /* kexec will return zero on success */
70         /* On error, force a kexec with the -f option */
71
72         if (result) {
73                 *(p + 0) = "-f";                /* 6 */
74                 *(p + 1) = l_image;             /* 7 */
75                 *(p + 2) = NULL;                /* 8 */
76
77                 result = pb_run_cmd(argv);
78         }
79
80         if (result)
81                 pb_log("%s: failed: (%d)\n", __func__, result);
82
83         return result;
84 }
85
86 /**
87  * pb_run_kexec - Run kexec with the supplied boot options.
88  *
89  * For the convenience of the user, tries to load both files before
90  * returning error.
91  */
92
93 int pb_run_kexec(const struct pb_kexec_data *kd)
94 {
95         int result;
96         char *l_image;
97         char *l_initrd;
98
99         pb_log("%s: image:  '%s'\n", __func__, kd->image);
100         pb_log("%s: initrd: '%s'\n", __func__, kd->initrd);
101         pb_log("%s: args:   '%s'\n", __func__, kd->args);
102
103         if (kd->image)
104                 l_image = pb_load_file(NULL, kd->image);
105         else {
106                 l_image = NULL;
107                 pb_log("%s: error null image\n", __func__);
108         }
109
110         l_initrd = kd->initrd ? pb_load_file(NULL, kd->initrd) : NULL;
111
112         if (!l_image || (kd->initrd && !l_initrd))
113                 result = -1;
114         else
115                 result = run_kexec_local(l_image, l_initrd, kd->args);
116
117         talloc_free(l_image);
118         talloc_free(l_initrd);
119
120         return result;
121 }
122
123 /**
124  * pb_elf_hash - Standard elf hash routine.
125  */
126
127 unsigned int pb_elf_hash(const char *str)
128 {
129         unsigned int h = 0, g;
130
131         while (*str) {
132                 h = (h << 4) + *str++;
133                 g = h & 0xf0000000;
134                 if (g)
135                         h ^= g >> 24;
136                 h &= ~g;
137         }
138         pb_log("%s: %x\n", __func__, h);
139         return h;
140 }
141
142 /**
143  * pb_cat_hash - Hashes concatenation of two strings.
144  */
145
146 unsigned int pb_cat_hash(const char *a, const char *b)
147 {
148         unsigned int h;
149         char *s;
150
151         s = talloc_asprintf(NULL, "%s%s", a, b);
152         h = pb_elf_hash(s);
153         talloc_free(s);
154
155         return h;
156 }