Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 2 | /* |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 3 | * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com> |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 909ff4d | 2019-08-01 09:46:45 -0600 | [diff] [blame] | 7 | #include <env.h> |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 8 | #include <linux/types.h> |
| 9 | #include <api_public.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 10 | #include <u-boot/crc.h> |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 11 | |
| 12 | #include "glue.h" |
| 13 | |
| 14 | static int valid_sig(struct api_signature *sig) |
| 15 | { |
| 16 | uint32_t checksum; |
| 17 | struct api_signature s; |
| 18 | |
| 19 | if (sig == NULL) |
| 20 | return 0; |
| 21 | /* |
| 22 | * Clear the checksum field (in the local copy) so as to calculate the |
| 23 | * CRC with the same initial contents as at the time when the sig was |
| 24 | * produced |
| 25 | */ |
| 26 | s = *sig; |
| 27 | s.checksum = 0; |
| 28 | |
| 29 | checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature)); |
| 30 | |
| 31 | if (checksum != sig->checksum) |
| 32 | return 0; |
| 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Searches for the U-Boot API signature |
| 39 | * |
| 40 | * returns 1/0 depending on found/not found result |
| 41 | */ |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 42 | int api_search_sig(struct api_signature **sig) |
| 43 | { |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 44 | unsigned char *sp; |
Rafal Jaworowski | 3d25ba5 | 2009-01-23 13:27:15 +0100 | [diff] [blame] | 45 | uint32_t search_start = 0; |
| 46 | uint32_t search_end = 0; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 47 | |
| 48 | if (sig == NULL) |
| 49 | return 0; |
| 50 | |
Rafal Jaworowski | 3d25ba5 | 2009-01-23 13:27:15 +0100 | [diff] [blame] | 51 | if (search_hint == 0) |
| 52 | search_hint = 255 * 1024 * 1024; |
| 53 | |
| 54 | search_start = search_hint & ~0x000fffff; |
| 55 | search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 56 | |
Rafal Jaworowski | 3d25ba5 | 2009-01-23 13:27:15 +0100 | [diff] [blame] | 57 | sp = (unsigned char *)search_start; |
| 58 | while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) { |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 59 | if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) { |
| 60 | *sig = (struct api_signature *)sp; |
| 61 | if (valid_sig(*sig)) |
| 62 | return 1; |
| 63 | } |
| 64 | sp += API_SIG_MAGLEN; |
| 65 | } |
| 66 | |
| 67 | *sig = NULL; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /**************************************** |
| 72 | * |
| 73 | * console |
| 74 | * |
| 75 | ****************************************/ |
| 76 | |
| 77 | int ub_getc(void) |
| 78 | { |
| 79 | int c; |
| 80 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 81 | if (!syscall(API_GETC, NULL, &c)) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 82 | return -1; |
| 83 | |
| 84 | return c; |
| 85 | } |
| 86 | |
| 87 | int ub_tstc(void) |
| 88 | { |
| 89 | int t; |
| 90 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 91 | if (!syscall(API_TSTC, NULL, &t)) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 92 | return -1; |
| 93 | |
| 94 | return t; |
| 95 | } |
| 96 | |
| 97 | void ub_putc(char c) |
| 98 | { |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 99 | syscall(API_PUTC, NULL, &c); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void ub_puts(const char *s) |
| 103 | { |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 104 | syscall(API_PUTS, NULL, s); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /**************************************** |
| 108 | * |
| 109 | * system |
| 110 | * |
| 111 | ****************************************/ |
| 112 | |
| 113 | void ub_reset(void) |
| 114 | { |
| 115 | syscall(API_RESET, NULL); |
| 116 | } |
| 117 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 118 | static struct mem_region mr[UB_MAX_MR]; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 119 | static struct sys_info si; |
| 120 | |
| 121 | struct sys_info * ub_get_sys_info(void) |
| 122 | { |
| 123 | int err = 0; |
| 124 | |
| 125 | memset(&si, 0, sizeof(struct sys_info)); |
| 126 | si.mr = mr; |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 127 | si.mr_no = UB_MAX_MR; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 128 | memset(&mr, 0, sizeof(mr)); |
| 129 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 130 | if (!syscall(API_GET_SYS_INFO, &err, &si)) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 131 | return NULL; |
| 132 | |
| 133 | return ((err) ? NULL : &si); |
| 134 | } |
| 135 | |
| 136 | /**************************************** |
| 137 | * |
| 138 | * timing |
| 139 | * |
| 140 | ****************************************/ |
Wolfgang Denk | 545b06a | 2008-01-10 00:55:14 +0100 | [diff] [blame] | 141 | |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 142 | void ub_udelay(unsigned long usec) |
| 143 | { |
| 144 | syscall(API_UDELAY, NULL, &usec); |
| 145 | } |
| 146 | |
| 147 | unsigned long ub_get_timer(unsigned long base) |
| 148 | { |
| 149 | unsigned long cur; |
| 150 | |
| 151 | if (!syscall(API_GET_TIMER, NULL, &cur, &base)) |
| 152 | return 0; |
| 153 | |
| 154 | return cur; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /**************************************************************************** |
| 159 | * |
| 160 | * devices |
| 161 | * |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 162 | * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1 |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 163 | * |
| 164 | ***************************************************************************/ |
| 165 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 166 | static struct device_info devices[UB_MAX_DEV]; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 167 | |
| 168 | struct device_info * ub_dev_get(int i) |
| 169 | { |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 170 | return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Enumerates the devices: fills out device_info elements in the devices[] |
| 175 | * array. |
| 176 | * |
| 177 | * returns: number of devices found |
| 178 | */ |
| 179 | int ub_dev_enum(void) |
| 180 | { |
| 181 | struct device_info *di; |
| 182 | int n = 0; |
| 183 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 184 | memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 185 | di = &devices[0]; |
| 186 | |
| 187 | if (!syscall(API_DEV_ENUM, NULL, di)) |
| 188 | return 0; |
| 189 | |
| 190 | while (di->cookie != NULL) { |
| 191 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 192 | if (++n >= UB_MAX_DEV) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 193 | break; |
| 194 | |
| 195 | /* take another device_info */ |
| 196 | di++; |
| 197 | |
| 198 | /* pass on the previous cookie */ |
| 199 | di->cookie = devices[n - 1].cookie; |
| 200 | |
| 201 | if (!syscall(API_DEV_ENUM, NULL, di)) |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | return n; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * handle: 0-based id of the device |
| 210 | * |
| 211 | * returns: 0 when OK, err otherwise |
| 212 | */ |
| 213 | int ub_dev_open(int handle) |
| 214 | { |
| 215 | struct device_info *di; |
| 216 | int err = 0; |
| 217 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 218 | if (handle < 0 || handle >= UB_MAX_DEV) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 219 | return API_EINVAL; |
| 220 | |
| 221 | di = &devices[handle]; |
| 222 | |
| 223 | if (!syscall(API_DEV_OPEN, &err, di)) |
| 224 | return -1; |
| 225 | |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | int ub_dev_close(int handle) |
| 230 | { |
| 231 | struct device_info *di; |
| 232 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 233 | if (handle < 0 || handle >= UB_MAX_DEV) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 234 | return API_EINVAL; |
| 235 | |
| 236 | di = &devices[handle]; |
| 237 | if (!syscall(API_DEV_CLOSE, NULL, di)) |
| 238 | return -1; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * |
| 245 | * Validates device for read/write, it has to: |
| 246 | * |
| 247 | * - have sane handle |
| 248 | * - be opened |
| 249 | * |
| 250 | * returns: 0/1 accordingly |
| 251 | */ |
| 252 | static int dev_valid(int handle) |
| 253 | { |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 254 | if (handle < 0 || handle >= UB_MAX_DEV) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 255 | return 0; |
| 256 | |
| 257 | if (devices[handle].state != DEV_STA_OPEN) |
| 258 | return 0; |
| 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | static int dev_stor_valid(int handle) |
| 264 | { |
| 265 | if (!dev_valid(handle)) |
| 266 | return 0; |
| 267 | |
| 268 | if (!(devices[handle].type & DEV_TYP_STOR)) |
| 269 | return 0; |
| 270 | |
| 271 | return 1; |
| 272 | } |
| 273 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 274 | int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start, |
| 275 | lbasize_t *rlen) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 276 | { |
| 277 | struct device_info *di; |
| 278 | lbasize_t act_len; |
| 279 | int err = 0; |
| 280 | |
| 281 | if (!dev_stor_valid(handle)) |
| 282 | return API_ENODEV; |
| 283 | |
| 284 | di = &devices[handle]; |
| 285 | if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len)) |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 286 | return API_ESYSC; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 287 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 288 | if (!err && rlen) |
| 289 | *rlen = act_len; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 290 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 291 | return err; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static int dev_net_valid(int handle) |
| 295 | { |
| 296 | if (!dev_valid(handle)) |
| 297 | return 0; |
| 298 | |
| 299 | if (devices[handle].type != DEV_TYP_NET) |
| 300 | return 0; |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 305 | int ub_dev_recv(int handle, void *buf, int len, int *rlen) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 306 | { |
| 307 | struct device_info *di; |
| 308 | int err = 0, act_len; |
| 309 | |
| 310 | if (!dev_net_valid(handle)) |
| 311 | return API_ENODEV; |
| 312 | |
| 313 | di = &devices[handle]; |
| 314 | if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len)) |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 315 | return API_ESYSC; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 316 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 317 | if (!err && rlen) |
| 318 | *rlen = act_len; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 319 | |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 320 | return (err); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | int ub_dev_send(int handle, void *buf, int len) |
| 324 | { |
| 325 | struct device_info *di; |
| 326 | int err = 0; |
| 327 | |
| 328 | if (!dev_net_valid(handle)) |
| 329 | return API_ENODEV; |
| 330 | |
| 331 | di = &devices[handle]; |
| 332 | if (!syscall(API_DEV_WRITE, &err, di, buf, &len)) |
Rafal Jaworowski | 6feb82d | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 333 | return API_ESYSC; |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 334 | |
| 335 | return err; |
| 336 | } |
| 337 | |
| 338 | /**************************************** |
| 339 | * |
| 340 | * env vars |
| 341 | * |
| 342 | ****************************************/ |
| 343 | |
| 344 | char * ub_env_get(const char *name) |
| 345 | { |
| 346 | char *value; |
| 347 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 348 | if (!syscall(API_ENV_GET, NULL, name, &value)) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 349 | return NULL; |
| 350 | |
| 351 | return value; |
| 352 | } |
| 353 | |
| 354 | void ub_env_set(const char *name, char *value) |
| 355 | { |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 356 | syscall(API_ENV_SET, NULL, name, value); |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 359 | static char env_name[256]; |
| 360 | |
| 361 | const char * ub_env_enum(const char *last) |
| 362 | { |
| 363 | const char *env, *str; |
| 364 | int i; |
| 365 | |
| 366 | env = NULL; |
| 367 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 368 | if (!syscall(API_ENV_ENUM, NULL, last, &env)) |
Rafal Jaworowski | a19be78 | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 369 | return NULL; |
| 370 | |
| 371 | if (!env) |
| 372 | /* no more env. variables to enumerate */ |
| 373 | return NULL; |
| 374 | |
| 375 | /* next enumerated env var */ |
| 376 | memset(env_name, 0, 256); |
| 377 | for (i = 0, str = env; *str != '=' && *str != '\0';) |
| 378 | env_name[i++] = *str++; |
| 379 | |
| 380 | env_name[i] = '\0'; |
| 381 | |
| 382 | return env_name; |
| 383 | } |
Che-Liang Chiou | 0d46041 | 2011-10-20 23:04:22 +0000 | [diff] [blame] | 384 | |
| 385 | /**************************************** |
| 386 | * |
| 387 | * display |
| 388 | * |
| 389 | ****************************************/ |
| 390 | |
| 391 | int ub_display_get_info(int type, struct display_info *di) |
| 392 | { |
| 393 | int err = 0; |
| 394 | |
Stanislav Galabov | 10c7a57 | 2016-02-17 15:23:31 +0200 | [diff] [blame] | 395 | if (!syscall(API_DISPLAY_GET_INFO, &err, type, di)) |
Che-Liang Chiou | 0d46041 | 2011-10-20 23:04:22 +0000 | [diff] [blame] | 396 | return API_ESYSC; |
| 397 | |
| 398 | return err; |
| 399 | } |
| 400 | |
| 401 | int ub_display_draw_bitmap(ulong bitmap, int x, int y) |
| 402 | { |
| 403 | int err = 0; |
| 404 | |
| 405 | if (!syscall(API_DISPLAY_DRAW_BITMAP, &err, bitmap, x, y)) |
| 406 | return API_ESYSC; |
| 407 | |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | void ub_display_clear(void) |
| 412 | { |
| 413 | syscall(API_DISPLAY_CLEAR, NULL); |
| 414 | } |
Rob Clark | faac73d | 2017-09-09 06:47:43 -0400 | [diff] [blame] | 415 | |
| 416 | __weak void *memcpy(void *dest, const void *src, size_t size) |
| 417 | { |
| 418 | unsigned char *dptr = dest; |
| 419 | const unsigned char *ptr = src; |
| 420 | const unsigned char *end = src + size; |
| 421 | |
| 422 | while (ptr < end) |
| 423 | *dptr++ = *ptr++; |
| 424 | |
| 425 | return dest; |
| 426 | } |