]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
e67ed00eec6b05de9845490c1b53241184675988
[petitboot] / discover / boot.c
1
2 #include <assert.h>
3
4 #include <log/log.h>
5 #include <pb-protocol/pb-protocol.h>
6 #include <system/system.h>
7 #include <talloc/talloc.h>
8
9 #include "boot.h"
10 #include "paths.h"
11
12 /**
13  * kexec_load - kexec load helper.
14  * @l_image: The local image file for kexec to execute.
15  * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL.
16  * @args: Optional command line args for kexec --append, can be NULL.
17  */
18
19 static int kexec_load(const char *l_image, const char *l_initrd,
20         const char *args, int dry_run)
21 {
22         int result;
23         const char *argv[6];
24         const char **p;
25         char *s_initrd = NULL;
26         char *s_args = NULL;
27
28         p = argv;
29         *p++ = pb_system_apps.kexec;    /* 1 */
30         *p++ = "-l";                    /* 2 */
31
32         if (l_initrd) {
33                 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
34                 assert(s_initrd);
35                 *p++ = s_initrd;         /* 3 */
36         }
37
38         if (args) {
39                 s_args = talloc_asprintf(NULL, "--append=%s", args);
40                 assert(s_args);
41                 *p++ = s_args;          /* 4 */
42         }
43
44         *p++ = l_image;                 /* 5 */
45         *p++ = NULL;                    /* 6 */
46
47         result = pb_run_cmd(argv, 1, dry_run);
48
49         if (result)
50                 pb_log("%s: failed: (%d)\n", __func__, result);
51
52         talloc_free(s_initrd);
53         talloc_free(s_args);
54
55         return result;
56 }
57
58 /**
59  * kexec_reboot - Helper to boot the new kernel.
60  *
61  * Must only be called after a successful call to kexec_load().
62  */
63
64 static int kexec_reboot(int dry_run)
65 {
66         int result = 0;
67         const char *argv[4];
68         const char **p;
69
70         /* First try running shutdown.  Init scripts should run 'exec -e' */
71
72         p = argv;
73         *p++ = pb_system_apps.shutdown; /* 1 */
74         *p++ =  "-r";                   /* 2 */
75         *p++ =  "now";                  /* 3 */
76         *p++ =  NULL;                   /* 4 */
77
78         result = pb_run_cmd(argv, 1, dry_run);
79
80         /* On error, force a kexec with the -e option */
81
82         if (result) {
83                 p = argv;
84                 *p++ = pb_system_apps.kexec;    /* 1 */
85                 *p++ = "-e";                    /* 2 */
86                 *p++ = NULL;                    /* 3 */
87
88                 result = pb_run_cmd(argv, 1, 0);
89         }
90
91         if (result)
92                 pb_log("%s: failed: (%d)\n", __func__, result);
93
94         return result;
95 }
96
97 int boot(void *ctx, struct boot_option *opt, struct boot_command *cmd,
98                 int dry_run)
99 {
100         char *local_image, *local_initrd;
101         unsigned int clean_image = 0;
102         unsigned int clean_initrd = 0;
103         char *image, *initrd, *args;
104         int result;
105
106         local_initrd = NULL;
107         image = NULL;
108         initrd = NULL;
109         args = NULL;
110
111         if (cmd->boot_image_file) {
112                 image = talloc_strdup(ctx, cmd->boot_image_file);
113         } else if (opt && opt->boot_image_file) {
114                 image = talloc_strdup(ctx, opt->boot_image_file);
115         } else {
116                 pb_log("%s: no image specified", __func__);
117                 return -1;
118         }
119
120         if (cmd->initrd_file) {
121                 initrd = talloc_strdup(ctx, cmd->initrd_file);
122         } else if (opt && opt->initrd_file) {
123                 initrd = talloc_strdup(ctx, opt->initrd_file);
124         }
125
126         if (cmd->boot_args) {
127                 args = talloc_strdup(ctx, cmd->boot_args);
128         } else if (opt && opt->boot_args) {
129                 args = talloc_strdup(ctx, opt->boot_args);
130         }
131
132         result = -1;
133
134         local_image = load_file(NULL, image, &clean_image);
135         if (!local_image)
136                 goto no_load;
137
138         if (initrd) {
139                 local_initrd = load_file(NULL, initrd, &clean_initrd);
140                 if (!local_initrd)
141                         goto no_load;
142         }
143
144         result = kexec_load(local_image, local_initrd, args, dry_run);
145
146 no_load:
147         if (clean_image)
148                 unlink(local_image);
149         if (clean_initrd)
150                 unlink(local_initrd);
151
152         talloc_free(local_image);
153         talloc_free(local_initrd);
154
155         if (!result)
156                 result = kexec_reboot(dry_run);
157
158         return result;
159 }