]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
895f0ecd73cc54ccea2420c037b26124f9a68076
[petitboot] / discover / platform-powerpc.c
1
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/fcntl.h>
10 #include <sys/stat.h>
11
12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
15 #include <log/log.h>
16 #include <process/process.h>
17
18 #include "platform.h"
19 #include "ipmi.h"
20 #include "dt.h"
21
22 static const char *partition = "common";
23 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
24 static const char *devtree_dir = "/proc/device-tree/";
25 static const int ipmi_timeout = 500; /* milliseconds */
26
27 struct param {
28         char                    *name;
29         char                    *value;
30         bool                    modified;
31         struct list_item        list;
32 };
33
34 struct platform_powerpc {
35         struct list     params;
36         struct ipmi     *ipmi;
37         bool            ipmi_bootdev_persistent;
38         int             (*get_ipmi_bootdev)(
39                                 struct platform_powerpc *platform,
40                                 uint8_t *bootdev, bool *persistent);
41         int             (*clear_ipmi_bootdev)(
42                                 struct platform_powerpc *platform);
43         int             (*set_os_boot_sensor)(
44                                 struct platform_powerpc *platform);
45 };
46
47 static const char *known_params[] = {
48         "auto-boot?",
49         "petitboot,network",
50         "petitboot,timeout",
51         "petitboot,bootdev",
52         "petitboot,language",
53         "petitboot,debug?",
54         NULL,
55 };
56
57 #define to_platform_powerpc(p) \
58         (struct platform_powerpc *)(p->platform_data)
59
60 /* a partition max a max size of 64k * 16bytes = 1M */
61 static const int max_partition_size = 64 * 1024 * 16;
62
63 static bool param_is_known(const char *param, unsigned int len)
64 {
65         const char *known_param;
66         unsigned int i;
67
68         for (i = 0; known_params[i]; i++) {
69                 known_param = known_params[i];
70                 if (len == strlen(known_param) &&
71                                 !strncmp(param, known_param, len))
72                         return true;
73         }
74
75         return false;
76 }
77
78 static int parse_nvram_params(struct platform_powerpc *platform,
79                 char *buf, int len)
80 {
81         char *pos, *name, *value;
82         unsigned int paramlen;
83         int i, count;
84
85         /* discard 2 header lines:
86          * "common" partiton"
87          * ------------------
88          */
89         pos = buf;
90         count = 0;
91
92         for (i = 0; i < len; i++) {
93                 if (pos[i] == '\n')
94                         count++;
95                 if (count == 2)
96                         break;
97         }
98
99         if (i == len) {
100                 fprintf(stderr, "failure parsing nvram output\n");
101                 return -1;
102         }
103
104         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
105                 unsigned int namelen;
106                 struct param *param;
107                 char *newline;
108
109                 newline = strchr(pos, '\n');
110                 if (!newline)
111                         break;
112
113                 *newline = '\0';
114
115                 paramlen = strlen(pos);
116
117                 name = pos;
118                 value = strchr(pos, '=');
119                 if (!value)
120                         continue;
121
122                 namelen = value - name;
123                 if (namelen == 0)
124                         continue;
125
126                 if (!param_is_known(name, namelen))
127                         continue;
128
129                 value++;
130
131                 param = talloc(platform, struct param);
132                 param->modified = false;
133                 param->name = talloc_strndup(platform, name, namelen);
134                 param->value = talloc_strdup(platform, value);
135                 list_add(&platform->params, &param->list);
136         }
137
138         return 0;
139 }
140
141 static int parse_nvram(struct platform_powerpc *platform)
142 {
143         struct process *process;
144         const char *argv[5];
145         int rc;
146
147         argv[0] = "nvram";
148         argv[1] = "--print-config";
149         argv[2] = "--partition";
150         argv[3] = partition;
151         argv[4] = NULL;
152
153         process = process_create(platform);
154         process->path = "nvram";
155         process->argv = argv;
156         process->keep_stdout = true;
157
158         rc = process_run_sync(process);
159
160         if (rc || !process_exit_ok(process)) {
161                 fprintf(stderr, "nvram process returned "
162                                 "non-zero exit status\n");
163                 rc = -1;
164         } else {
165                 rc = parse_nvram_params(platform, process->stdout_buf,
166                                             process->stdout_len);
167         }
168
169         process_release(process);
170         return rc;
171 }
172
173 static int write_nvram(struct platform_powerpc *platform)
174 {
175         struct process *process;
176         struct param *param;
177         const char *argv[6];
178         int rc;
179
180         argv[0] = "nvram";
181         argv[1] = "--update-config";
182         argv[2] = NULL;
183         argv[3] = "--partition";
184         argv[4] = partition;
185         argv[5] = NULL;
186
187         process = process_create(platform);
188         process->path = "nvram";
189         process->argv = argv;
190
191         list_for_each_entry(&platform->params, param, list) {
192                 char *paramstr;
193
194                 if (!param->modified)
195                         continue;
196
197                 paramstr = talloc_asprintf(platform, "%s=%s",
198                                 param->name, param->value);
199                 argv[2] = paramstr;
200
201                 rc = process_run_sync(process);
202
203                 talloc_free(paramstr);
204
205                 if (rc || !process_exit_ok(process)) {
206                         rc = -1;
207                         pb_log("nvram update process returned "
208                                         "non-zero exit status\n");
209                         break;
210                 }
211         }
212
213         process_release(process);
214         return rc;
215 }
216
217 static const char *get_param(struct platform_powerpc *platform,
218                 const char *name)
219 {
220         struct param *param;
221
222         list_for_each_entry(&platform->params, param, list)
223                 if (!strcmp(param->name, name))
224                         return param->value;
225         return NULL;
226 }
227
228 static void set_param(struct platform_powerpc *platform, const char *name,
229                 const char *value)
230 {
231         struct param *param;
232
233         list_for_each_entry(&platform->params, param, list) {
234                 if (strcmp(param->name, name))
235                         continue;
236
237                 if (!strcmp(param->value, value))
238                         return;
239
240                 talloc_free(param->value);
241                 param->value = talloc_strdup(param, value);
242                 param->modified = true;
243                 return;
244         }
245
246
247         param = talloc(platform, struct param);
248         param->modified = true;
249         param->name = talloc_strdup(platform, name);
250         param->value = talloc_strdup(platform, value);
251         list_add(&platform->params, &param->list);
252 }
253
254 static int parse_hwaddr(struct interface_config *ifconf, char *str)
255 {
256         int i;
257
258         if (strlen(str) != strlen("00:00:00:00:00:00"))
259                 return -1;
260
261         for (i = 0; i < HWADDR_SIZE; i++) {
262                 char byte[3], *endp;
263                 unsigned long x;
264
265                 byte[0] = str[i * 3 + 0];
266                 byte[1] = str[i * 3 + 1];
267                 byte[2] = '\0';
268
269                 x = strtoul(byte, &endp, 16);
270                 if (endp != byte + 2)
271                         return -1;
272
273                 ifconf->hwaddr[i] = x & 0xff;
274         }
275
276         return 0;
277 }
278
279 static int parse_one_interface_config(struct config *config,
280                 char *confstr)
281 {
282         struct interface_config *ifconf;
283         char *tok, *saveptr;
284
285         ifconf = talloc_zero(config, struct interface_config);
286
287         if (!confstr || !strlen(confstr))
288                 goto out_err;
289
290         /* first token should be the mac address */
291         tok = strtok_r(confstr, ",", &saveptr);
292         if (!tok)
293                 goto out_err;
294
295         if (parse_hwaddr(ifconf, tok))
296                 goto out_err;
297
298         /* second token is the method */
299         tok = strtok_r(NULL, ",", &saveptr);
300         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
301                 ifconf->ignore = true;
302
303         } else if (!strcmp(tok, "dhcp")) {
304                 ifconf->method = CONFIG_METHOD_DHCP;
305
306         } else if (!strcmp(tok, "static")) {
307                 ifconf->method = CONFIG_METHOD_STATIC;
308
309                 /* ip/mask, [optional] gateway */
310                 tok = strtok_r(NULL, ",", &saveptr);
311                 if (!tok)
312                         goto out_err;
313                 ifconf->static_config.address =
314                         talloc_strdup(ifconf, tok);
315
316                 tok = strtok_r(NULL, ",", &saveptr);
317                 if (tok) {
318                         ifconf->static_config.gateway =
319                                 talloc_strdup(ifconf, tok);
320                 }
321
322         } else {
323                 pb_log("Unknown network configuration method %s\n", tok);
324                 goto out_err;
325         }
326
327         config->network.interfaces = talloc_realloc(config,
328                         config->network.interfaces,
329                         struct interface_config *,
330                         ++config->network.n_interfaces);
331
332         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
333
334         return 0;
335 out_err:
336         talloc_free(ifconf);
337         return -1;
338 }
339
340 static int parse_one_dns_config(struct config *config,
341                 char *confstr)
342 {
343         char *tok, *saveptr = NULL;
344
345         for (tok = strtok_r(confstr, ",", &saveptr); tok;
346                         tok = strtok_r(NULL, ",", &saveptr)) {
347
348                 char *server = talloc_strdup(config, tok);
349
350                 config->network.dns_servers = talloc_realloc(config,
351                                 config->network.dns_servers, const char *,
352                                 ++config->network.n_dns_servers);
353
354                 config->network.dns_servers[config->network.n_dns_servers - 1]
355                                 = server;
356         }
357
358         return 0;
359 }
360
361 static void populate_network_config(struct platform_powerpc *platform,
362                 struct config *config)
363 {
364         char *val, *saveptr = NULL;
365         const char *cval;
366         int i;
367
368         cval = get_param(platform, "petitboot,network");
369         if (!cval || !strlen(cval))
370                 return;
371
372         val = talloc_strdup(config, cval);
373
374         for (i = 0; ; i++) {
375                 char *tok;
376
377                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
378                 if (!tok)
379                         break;
380
381                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
382                         parse_one_dns_config(config, tok + strlen("dns,"));
383                 else
384                         parse_one_interface_config(config, tok);
385
386         }
387
388         talloc_free(val);
389 }
390
391 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
392 {
393         char *delim = strchr(*pos, ' ');
394         int len, prefix = 0, rc = -1;
395         enum device_type type;
396
397         if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
398                 prefix = strlen("uuid:");
399                 opt->boot_type = BOOT_DEVICE_UUID;
400                 rc = 0;
401         } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
402                 prefix = strlen("mac:");
403                 opt->boot_type = BOOT_DEVICE_UUID;
404                 rc = 0;
405         } else {
406                 type = find_device_type(*pos);
407                 if (type != DEVICE_TYPE_UNKNOWN) {
408                         opt->type = type;
409                         opt->boot_type = BOOT_DEVICE_TYPE;
410                         rc = 0;
411                 }
412         }
413
414         if (opt->boot_type == BOOT_DEVICE_UUID) {
415                 if (delim)
416                         len = (int)(delim - *pos) - prefix;
417                 else
418                         len = strlen(*pos);
419
420                 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
421         }
422
423         /* Always advance pointer to next option or end */
424         if (delim)
425                 *pos = delim + 1;
426         else
427                 *pos += strlen(*pos);
428
429         return rc;
430 }
431
432 static void populate_bootdev_config(struct platform_powerpc *platform,
433                 struct config *config)
434
435 {
436         const char *val;
437
438         config->boot_device = NULL;
439
440         val = get_param(platform, "petitboot,bootdev");
441         if (!val || !strlen(val))
442                 return;
443
444         if (!strncmp(val, "uuid:", strlen("uuid:"))) {
445                 config->boot_device = talloc_strdup(config,
446                                                 val + strlen("uuid:"));
447
448         } else if (!strncmp(val, "mac:", strlen("mac:"))) {
449                 config->boot_device = talloc_strdup(config,
450                                                 val + strlen("mac:"));
451
452         } else {
453                 pb_log("bootdev config is in an unknown format "
454                                 "(expected uuid:... or mac:...)");
455         }
456 }
457
458 static void populate_config(struct platform_powerpc *platform,
459                 struct config *config)
460 {
461         const char *val;
462         char *end;
463         unsigned long timeout;
464
465         /* if the "auto-boot?' property is present and "false", disable auto
466          * boot */
467         val = get_param(platform, "auto-boot?");
468         config->autoboot_enabled = !val || strcmp(val, "false");
469
470         val = get_param(platform, "petitboot,timeout");
471         if (val) {
472                 timeout = strtoul(val, &end, 10);
473                 if (end != val) {
474                         if (timeout >= INT_MAX)
475                                 timeout = INT_MAX;
476                         config->autoboot_timeout_sec = (int)timeout;
477                 }
478         }
479
480         val = get_param(platform, "petitboot,language");
481         config->lang = val ? talloc_strdup(config, val) : NULL;
482
483         populate_network_config(platform, config);
484
485         populate_bootdev_config(platform, config);
486
487         if (!config->debug) {
488                 val = get_param(platform, "petitboot,debug?");
489                 config->debug = val && !strcmp(val, "true");
490         }
491 }
492
493 static char *iface_config_str(void *ctx, struct interface_config *config)
494 {
495         char *str;
496
497         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
498          * different hardware address formats */
499         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
500                         config->hwaddr[0], config->hwaddr[1],
501                         config->hwaddr[2], config->hwaddr[3],
502                         config->hwaddr[4], config->hwaddr[5]);
503
504         if (config->ignore) {
505                 str = talloc_asprintf_append(str, "ignore");
506
507         } else if (config->method == CONFIG_METHOD_DHCP) {
508                 str = talloc_asprintf_append(str, "dhcp");
509
510         } else if (config->method == CONFIG_METHOD_STATIC) {
511                 str = talloc_asprintf_append(str, "static,%s%s%s",
512                                 config->static_config.address,
513                                 config->static_config.gateway ? "," : "",
514                                 config->static_config.gateway ?: "");
515         }
516         return str;
517 }
518
519 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
520 {
521         char *str;
522         int i;
523
524         str = talloc_strdup(ctx, "dns,");
525         for (i = 0; i < n; i++) {
526                 str = talloc_asprintf_append(str, "%s%s",
527                                 i == 0 ? "" : ",",
528                                 dns_servers[i]);
529         }
530
531         return str;
532 }
533
534 static void update_string_config(struct platform_powerpc *platform,
535                 const char *name, const char *value)
536 {
537         const char *cur;
538
539         cur = get_param(platform, name);
540
541         /* don't set an empty parameter if it doesn't already exist */
542         if (!cur && !strlen(value))
543                 return;
544
545         set_param(platform, name, value);
546 }
547
548 static void update_network_config(struct platform_powerpc *platform,
549         struct config *config)
550 {
551         unsigned int i;
552         char *val;
553
554         val = talloc_strdup(platform, "");
555
556         for (i = 0; i < config->network.n_interfaces; i++) {
557                 char *iface_str = iface_config_str(platform,
558                                         config->network.interfaces[i]);
559                 val = talloc_asprintf_append(val, "%s%s",
560                                 *val == '\0' ? "" : " ", iface_str);
561                 talloc_free(iface_str);
562         }
563
564         if (config->network.n_dns_servers) {
565                 char *dns_str = dns_config_str(platform,
566                                                 config->network.dns_servers,
567                                                 config->network.n_dns_servers);
568                 val = talloc_asprintf_append(val, "%s%s",
569                                 *val == '\0' ? "" : " ", dns_str);
570                 talloc_free(dns_str);
571         }
572
573         update_string_config(platform, "petitboot,network", val);
574
575         talloc_free(val);
576 }
577
578 static void update_bootdev_config(struct platform_powerpc *platform,
579                 struct config *config)
580 {
581         char *val, *tmp = NULL;
582
583         if (!config->boot_device)
584                 val = "";
585         else
586                 tmp = val = talloc_asprintf(platform,
587                                 "uuid:%s", config->boot_device);
588
589         update_string_config(platform, "petitboot,bootdev", val);
590         talloc_free(tmp);
591 }
592
593 static int update_config(struct platform_powerpc *platform,
594                 struct config *config, struct config *defaults)
595 {
596         char *tmp = NULL;
597         const char *val;
598
599         if (config->autoboot_enabled == defaults->autoboot_enabled)
600                 val = "";
601         else
602                 val = config->autoboot_enabled ? "true" : "false";
603         update_string_config(platform, "auto-boot?", val);
604
605         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
606                 val = "";
607         else
608                 val = tmp = talloc_asprintf(platform, "%d",
609                                 config->autoboot_timeout_sec);
610
611         update_string_config(platform, "petitboot,timeout", val);
612         if (tmp)
613                 talloc_free(tmp);
614
615         val = config->lang ?: "";
616         update_string_config(platform, "petitboot,language", val);
617
618         update_network_config(platform, config);
619
620         update_bootdev_config(platform, config);
621
622         return write_nvram(platform);
623 }
624
625 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
626                 bool persistent)
627 {
628         config->ipmi_bootdev = bootdev;
629         config->ipmi_bootdev_persistent = persistent;
630
631         switch (bootdev) {
632         case IPMI_BOOTDEV_NONE:
633         case IPMI_BOOTDEV_DISK:
634         case IPMI_BOOTDEV_NETWORK:
635         case IPMI_BOOTDEV_CDROM:
636                 break;
637         case IPMI_BOOTDEV_SETUP:
638                 config->autoboot_enabled = false;
639                 break;
640         case IPMI_BOOTDEV_SAFE:
641                 config->autoboot_enabled = false;
642                 config->safe_mode = true;
643                 break;
644         }
645 }
646
647 static int read_bootdev_sysparam(const char *name, uint8_t *val)
648 {
649         uint8_t buf[2];
650         char path[50];
651         int fd, rc;
652
653         strcpy(path, sysparams_dir);
654         assert(strlen(name) < sizeof(path) - strlen(path));
655         strcat(path, name);
656
657         fd = open(path, O_RDONLY);
658         if (fd < 0) {
659                 pb_debug("powerpc: can't access sysparam %s\n",
660                                 name);
661                 return -1;
662         }
663
664         rc = read(fd, buf, sizeof(buf));
665
666         close(fd);
667
668         /* bootdev definitions should only be one byte in size */
669         if (rc != 1) {
670                 pb_debug("powerpc: sysparam %s read returned %d\n",
671                                 name, rc);
672                 return -1;
673         }
674
675         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
676
677         if (!ipmi_bootdev_is_valid(buf[0]))
678                 return -1;
679
680         *val = buf[0];
681         return 0;
682 }
683
684 static int write_bootdev_sysparam(const char *name, uint8_t val)
685 {
686         char path[50];
687         int fd, rc;
688
689         strcpy(path, sysparams_dir);
690         assert(strlen(name) < sizeof(path) - strlen(path));
691         strcat(path, name);
692
693         fd = open(path, O_WRONLY);
694         if (fd < 0) {
695                 pb_debug("powerpc: can't access sysparam %s for writing\n",
696                                 name);
697                 return -1;
698         }
699
700         for (;;) {
701                 errno = 0;
702                 rc = write(fd, &val, sizeof(val));
703                 if (rc == sizeof(val)) {
704                         rc = 0;
705                         break;
706                 }
707
708                 if (rc <= 0 && errno != EINTR) {
709                         pb_log("powerpc: error updating sysparam %s: %s",
710                                         name, strerror(errno));
711                         rc = -1;
712                         break;
713                 }
714         }
715
716         close(fd);
717
718         if (!rc)
719                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
720
721         return rc;
722 }
723
724 static int clear_ipmi_bootdev_sysparams(
725                 struct platform_powerpc *platform __attribute__((unused)))
726 {
727         /* invalidate next-boot-device setting */
728         write_bootdev_sysparam("next-boot-device", 0xff);
729         return 0;
730 }
731
732 static int get_ipmi_bootdev_sysparams(
733                 struct platform_powerpc *platform __attribute__((unused)),
734                 uint8_t *bootdev, bool *persistent)
735 {
736         uint8_t next_bootdev, default_bootdev;
737         bool next_valid, default_valid;
738         int rc;
739
740         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
741         next_valid = rc == 0;
742
743         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
744         default_valid = rc == 0;
745
746         /* nothing valid? no need to change the config */
747         if (!next_valid && !default_valid)
748                 return -1;
749
750         *persistent = !next_valid;
751         *bootdev = next_valid ? next_bootdev : default_bootdev;
752         return 0;
753 }
754
755 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform)
756 {
757         uint16_t resp_len;
758         uint8_t resp[1];
759         uint8_t req[] = {
760                 0x05, /* parameter selector: boot flags */
761                 0x80, /* data 1: valid */
762                 0x00, /* data 2: bootdev: no override */
763                 0x00, /* data 3: system defaults */
764                 0x00, /* data 4: no request for shared mode, mux defaults */
765                 0x00, /* data 5: no instance request */
766         };
767
768         resp_len = sizeof(resp);
769
770         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
771                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
772                         req, sizeof(req),
773                         resp, &resp_len,
774                         ipmi_timeout);
775         return 0;
776 }
777
778 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
779                 uint8_t *bootdev, bool *persistent)
780 {
781         uint16_t resp_len;
782         uint8_t resp[8];
783         int rc;
784         uint8_t req[] = {
785                 0x05, /* parameter selector: boot flags */
786                 0x00, /* no set selector */
787                 0x00, /* no block selector */
788         };
789
790         resp_len = sizeof(resp);
791         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
792                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
793                         req, sizeof(req),
794                         resp, &resp_len,
795                         ipmi_timeout);
796         if (rc) {
797                 pb_log("platform: error reading IPMI boot options\n");
798                 return -1;
799         }
800
801         if (resp_len != sizeof(resp)) {
802                 pb_log("platform: unexpected length (%d) in "
803                                 "boot options response\n", resp_len);
804                 return -1;
805         }
806
807         if (resp[0] != 0) {
808                 pb_log("platform: non-zero completion code %d from IPMI req\n",
809                                 resp[0]);
810                 return -1;
811         }
812
813         /* check for correct parameter version */
814         if ((resp[1] & 0xf) != 0x1) {
815                 pb_log("platform: unexpected version (0x%x) in "
816                                 "boot options response\n", resp[0]);
817                 return -1;
818         }
819
820         /* check for valid paramters */
821         if (resp[2] & 0x80) {
822                 pb_debug("platform: boot options are invalid/locked\n");
823                 return -1;
824         }
825
826         *persistent = false;
827
828         /* check for valid flags */
829         if (!(resp[3] & 0x80)) {
830                 pb_debug("platform: boot flags are invalid, ignoring\n");
831                 return 0;
832         }
833
834         *persistent = resp[3] & 0x40;
835         *bootdev = (resp[4] >> 2) & 0x0f;
836         return 0;
837 }
838
839 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
840 {
841         int sensor_number;
842         uint16_t resp_len;
843         uint8_t resp[1];
844         uint8_t req[] = {
845                 0x00, /* sensor number: os boot */
846                 0xA9, /* operation: set everything */
847                 0x00, /* sensor reading: none */
848                 0x40, /* assertion mask lsb: set state 6 */
849                 0x00, /* assertion mask msb: none */
850                 0x00, /* deassertion mask lsb: none */
851                 0x00, /* deassertion mask msb: none */
852                 0x00, /* event data 1: none */
853                 0x00, /* event data 2: none */
854                 0x00, /* event data 3: none */
855         };
856
857         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
858         if (sensor_number < 0) {
859                 pb_log("Couldn't find OS boot sensor in device tree\n");
860                 return -1;
861         }
862
863         req[0] = sensor_number;
864
865         resp_len = sizeof(resp);
866
867         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
868                         IPMI_CMD_SENSOR_SET,
869                         req, sizeof(req),
870                         resp, &resp_len,
871                         ipmi_timeout); return 0;
872
873         return 0;
874 }
875
876 static int load_config(struct platform *p, struct config *config)
877 {
878         struct platform_powerpc *platform = to_platform_powerpc(p);
879         int rc;
880
881         rc = parse_nvram(platform);
882         if (rc)
883                 return rc;
884
885         populate_config(platform, config);
886
887         if (platform->get_ipmi_bootdev) {
888                 bool bootdev_persistent;
889                 uint8_t bootdev;
890                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
891                                 &bootdev_persistent);
892                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
893                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
894                 }
895         }
896
897         return 0;
898 }
899
900 static int save_config(struct platform *p, struct config *config)
901 {
902         struct platform_powerpc *platform = to_platform_powerpc(p);
903         struct config *defaults;
904         int rc;
905
906         defaults = talloc_zero(platform, struct config);
907         config_set_defaults(defaults);
908
909         rc = update_config(platform, config, defaults);
910
911         talloc_free(defaults);
912         return rc;
913 }
914
915 static void pre_boot(struct platform *p, const struct config *config)
916 {
917         struct platform_powerpc *platform = to_platform_powerpc(p);
918
919         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
920                 platform->clear_ipmi_bootdev(platform);
921
922         if (platform->set_os_boot_sensor)
923                 platform->set_os_boot_sensor(platform);
924 }
925
926 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
927 {
928         struct platform_powerpc *platform = p->platform_data;
929         char *buf, *filename;
930         int len, rc;
931
932         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
933         rc = read_file(platform, filename, &buf, &len);
934         if (rc == 0)
935                 sysinfo->type = talloc_steal(sysinfo, buf);
936         talloc_free(filename);
937
938         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
939         rc = read_file(platform, filename, &buf, &len);
940         if (rc == 0)
941                 sysinfo->identifier = talloc_steal(sysinfo, buf);
942         talloc_free(filename);
943
944         return 0;
945 }
946
947 static bool probe(struct platform *p, void *ctx)
948 {
949         struct platform_powerpc *platform;
950         struct stat statbuf;
951         int rc;
952
953         /* we need a device tree */
954         rc = stat("/proc/device-tree", &statbuf);
955         if (rc)
956                 return false;
957
958         if (!S_ISDIR(statbuf.st_mode))
959                 return false;
960
961         platform = talloc_zero(ctx, struct platform_powerpc);
962         list_init(&platform->params);
963
964         p->platform_data = platform;
965
966         if (ipmi_present()) {
967                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
968                 platform->ipmi = ipmi_open(platform);
969                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
970                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
971                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
972
973         } else if (!stat(sysparams_dir, &statbuf)) {
974                 pb_debug("platform: using sysparams for IPMI paramters\n");
975                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
976                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
977
978         } else {
979                 pb_log("platform: no IPMI parameter support\n");
980         }
981
982         return true;
983 }
984
985
986 static struct platform platform_powerpc = {
987         .name                   = "powerpc",
988         .dhcp_arch_id           = 0x000e,
989         .probe                  = probe,
990         .load_config            = load_config,
991         .save_config            = save_config,
992         .pre_boot               = pre_boot,
993         .get_sysinfo            = get_sysinfo,
994 };
995
996 register_platform(platform_powerpc);