Clone a tag rather than SeaBIOS stable branch HEAD
[coreboot.git] / payloads / bayou / payload.c
1 /*
2  * This file is part of the bayou project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include "bayou.h"
21
22 #define TIMEOUT_MESSAGE "Press ESC for the menu (%2d)...\r"
23 #define TIMEOUT_KEY     '\033'
24
25 void run_payload(struct payload *p)
26 {
27         int ret, i;
28
29         /* For chooser entries, just run the payload. */
30         if (p->pentry.type == BPT_TYPE_CHOOSER) {
31                 self_load_and_run(p, &ret);
32                 return;
33         }
34
35         /* For chained entries, run all the sub-chain items. */
36         for (i = 0; i < bayoucfg.n_entries; i++) {
37                 struct payload *s = &(bayoucfg.entries[i]);
38
39                 if (s->pentry.parent == p->pentry.index)
40                         self_load_and_run(s, &ret);
41         }
42 }
43
44 char *payload_get_name(struct payload *p)
45 {
46         if (p->pentry.type == BPT_TYPE_CHAIN)
47                 return (char *)p->pentry.title;
48         else if (p->pentry.type == BPT_TYPE_CHOOSER) {
49                 if (p->pentry.title[0] != 0)
50                         return (char *)p->pentry.title;
51                 return p->params[BAYOU_PARAM_DESC];
52         }
53
54         return NULL;
55 }
56
57 struct payload *payload_get_default(void)
58 {
59         int i;
60
61         for (i = 0; i < bayoucfg.n_entries; i++) {
62                 struct payload *s = &(bayoucfg.entries[i]);
63
64                 if (s->pentry.parent == 0 && s->pentry.flags & BPT_FLAG_DEFAULT)
65                         return s;
66         }
67
68         return NULL;
69 }
70
71 void run_payload_timeout(struct payload *p, int timeout)
72 {
73         int t, ch, tval;
74
75         for (t = timeout; t >= 0; t--) {
76                 printf(TIMEOUT_MESSAGE, t);
77
78                 tval = 1000;
79                 ch = getchar_timeout(&tval);
80
81                 if (ch == TIMEOUT_KEY)
82                         return;
83         }
84
85         run_payload(p);
86 }
87
88 void payload_parse_params(struct payload *pload, u8 *params, int len)
89 {
90         char *ptr = (char *)params;
91         int i = 0;
92
93         if (ptr == NULL)
94                 return;
95
96         while (ptr < ((char *)params + len)) {
97
98                 if (!strncmp(ptr, "name=", 5)) {
99                         pload->params[BAYOU_PARAM_NAME] = ptr + 5;
100                 } else if (!strncmp(ptr, "desc=", 5)) {
101                         pload->params[BAYOU_PARAM_DESC] = ptr + 5;
102                 } else if (!strncmp(ptr, "listname=", 9)) {
103                         pload->params[BAYOU_PARAM_LIST] = ptr + 9;
104                 }
105
106                 ptr += strnlen(ptr, len - i);
107
108                 if (ptr < ((char *)params + len) && *ptr == 0)
109                         ptr++;
110         }
111 }