]>
Commit | Line | Data |
---|---|---|
bc74112f GA |
1 | /* |
2 | * QEMU Boot Device Implement | |
3 | * | |
4 | * Copyright (c) 2014 HUAWEI TECHNOLOGIES CO.,LTD. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "sysemu/sysemu.h" | |
26 | ||
27 | typedef struct FWBootEntry FWBootEntry; | |
28 | ||
29 | struct FWBootEntry { | |
30 | QTAILQ_ENTRY(FWBootEntry) link; | |
31 | int32_t bootindex; | |
32 | DeviceState *dev; | |
33 | char *suffix; | |
34 | }; | |
35 | ||
36 | static QTAILQ_HEAD(, FWBootEntry) fw_boot_order = | |
37 | QTAILQ_HEAD_INITIALIZER(fw_boot_order); | |
38 | ||
694fb857 GA |
39 | void check_boot_index(int32_t bootindex, Error **errp) |
40 | { | |
41 | FWBootEntry *i; | |
42 | ||
43 | if (bootindex >= 0) { | |
44 | QTAILQ_FOREACH(i, &fw_boot_order, link) { | |
45 | if (i->bootindex == bootindex) { | |
46 | error_setg(errp, "The bootindex %d has already been used", | |
47 | bootindex); | |
48 | return; | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
9d27572d GA |
54 | void del_boot_device_path(DeviceState *dev, const char *suffix) |
55 | { | |
56 | FWBootEntry *i; | |
57 | ||
58 | if (dev == NULL) { | |
59 | return; | |
60 | } | |
61 | ||
62 | QTAILQ_FOREACH(i, &fw_boot_order, link) { | |
63 | if ((!suffix || !g_strcmp0(i->suffix, suffix)) && | |
64 | i->dev == dev) { | |
65 | QTAILQ_REMOVE(&fw_boot_order, i, link); | |
66 | g_free(i->suffix); | |
67 | g_free(i); | |
68 | ||
69 | break; | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
bc74112f GA |
74 | void add_boot_device_path(int32_t bootindex, DeviceState *dev, |
75 | const char *suffix) | |
76 | { | |
77 | FWBootEntry *node, *i; | |
78 | ||
79 | if (bootindex < 0) { | |
a598f2ff | 80 | del_boot_device_path(dev, suffix); |
bc74112f GA |
81 | return; |
82 | } | |
83 | ||
84 | assert(dev != NULL || suffix != NULL); | |
85 | ||
e614b54b GA |
86 | del_boot_device_path(dev, suffix); |
87 | ||
bc74112f GA |
88 | node = g_malloc0(sizeof(FWBootEntry)); |
89 | node->bootindex = bootindex; | |
90 | node->suffix = g_strdup(suffix); | |
91 | node->dev = dev; | |
92 | ||
93 | QTAILQ_FOREACH(i, &fw_boot_order, link) { | |
94 | if (i->bootindex == bootindex) { | |
95 | fprintf(stderr, "Two devices with same boot index %d\n", bootindex); | |
96 | exit(1); | |
97 | } else if (i->bootindex < bootindex) { | |
98 | continue; | |
99 | } | |
100 | QTAILQ_INSERT_BEFORE(i, node, link); | |
101 | return; | |
102 | } | |
103 | QTAILQ_INSERT_TAIL(&fw_boot_order, node, link); | |
104 | } | |
105 | ||
106 | DeviceState *get_boot_device(uint32_t position) | |
107 | { | |
108 | uint32_t counter = 0; | |
109 | FWBootEntry *i = NULL; | |
110 | DeviceState *res = NULL; | |
111 | ||
112 | if (!QTAILQ_EMPTY(&fw_boot_order)) { | |
113 | QTAILQ_FOREACH(i, &fw_boot_order, link) { | |
114 | if (counter == position) { | |
115 | res = i->dev; | |
116 | break; | |
117 | } | |
118 | counter++; | |
119 | } | |
120 | } | |
121 | return res; | |
122 | } | |
123 | ||
124 | /* | |
125 | * This function returns null terminated string that consist of new line | |
126 | * separated device paths. | |
127 | * | |
128 | * memory pointed by "size" is assigned total length of the array in bytes | |
129 | * | |
130 | */ | |
131 | char *get_boot_devices_list(size_t *size, bool ignore_suffixes) | |
132 | { | |
133 | FWBootEntry *i; | |
134 | size_t total = 0; | |
135 | char *list = NULL; | |
136 | ||
137 | QTAILQ_FOREACH(i, &fw_boot_order, link) { | |
138 | char *devpath = NULL, *bootpath; | |
139 | size_t len; | |
140 | ||
141 | if (i->dev) { | |
142 | devpath = qdev_get_fw_dev_path(i->dev); | |
143 | assert(devpath); | |
144 | } | |
145 | ||
146 | if (i->suffix && !ignore_suffixes && devpath) { | |
147 | size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1; | |
148 | ||
149 | bootpath = g_malloc(bootpathlen); | |
150 | snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix); | |
151 | g_free(devpath); | |
152 | } else if (devpath) { | |
153 | bootpath = devpath; | |
154 | } else if (!ignore_suffixes) { | |
155 | assert(i->suffix); | |
156 | bootpath = g_strdup(i->suffix); | |
157 | } else { | |
158 | bootpath = g_strdup(""); | |
159 | } | |
160 | ||
161 | if (total) { | |
162 | list[total-1] = '\n'; | |
163 | } | |
164 | len = strlen(bootpath) + 1; | |
165 | list = g_realloc(list, total + len); | |
166 | memcpy(&list[total], bootpath, len); | |
167 | total += len; | |
168 | g_free(bootpath); | |
169 | } | |
170 | ||
171 | *size = total; | |
172 | ||
173 | if (boot_strict && *size > 0) { | |
174 | list[total-1] = '\n'; | |
175 | list = g_realloc(list, total + 5); | |
176 | memcpy(&list[total], "HALT", 5); | |
177 | *size = total + 5; | |
178 | } | |
179 | return list; | |
180 | } |