Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame^] | 1 | /* Copyright 2016 IBM |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <stdarg.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <syslog.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "common.h" |
| 28 | |
| 29 | void mbox_log_console(int p, const char *fmt, va_list args) |
| 30 | { |
| 31 | struct timespec time; |
| 32 | FILE *s = (p < LOG_WARNING) ? stdout : stderr; |
| 33 | |
| 34 | clock_gettime(CLOCK_REALTIME, &time); |
| 35 | |
| 36 | fprintf(s, "[%s %ld.%.9ld] ", PREFIX, time.tv_sec, time.tv_nsec); |
| 37 | |
| 38 | vfprintf(s, fmt, args); |
| 39 | } |
| 40 | |
| 41 | __attribute__((format(printf, 2, 3))) |
| 42 | void mbox_log(int p, const char *fmt, ...) |
| 43 | { |
| 44 | va_list args; |
| 45 | |
| 46 | va_start(args, fmt); |
| 47 | mbox_vlog(p, fmt, args); |
| 48 | va_end(args); |
| 49 | } |
| 50 | |
| 51 | uint16_t get_u16(uint8_t *ptr) |
| 52 | { |
| 53 | return *(uint16_t *)ptr; |
| 54 | } |
| 55 | |
| 56 | void put_u16(uint8_t *ptr, uint16_t val) |
| 57 | { |
| 58 | memcpy(ptr, &val, sizeof(val)); |
| 59 | } |
| 60 | |
| 61 | uint32_t get_u32(uint8_t *ptr) |
| 62 | { |
| 63 | return *(uint32_t *)ptr; |
| 64 | } |
| 65 | |
| 66 | void put_u32(uint8_t *ptr, uint32_t val) |
| 67 | { |
| 68 | memcpy(ptr, &val, sizeof(val)); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static bool is_pnor_part(const char *str) |
| 73 | { |
| 74 | return strcasestr(str, "pnor") != NULL; |
| 75 | } |
| 76 | |
| 77 | char *get_dev_mtd(void) |
| 78 | { |
| 79 | FILE *f; |
| 80 | char *ret = NULL, *pos = NULL; |
| 81 | char line[255]; |
| 82 | |
| 83 | f = fopen("/proc/mtd", "r"); |
| 84 | if (!f) |
| 85 | return NULL; |
| 86 | |
| 87 | while (!pos && fgets(line, sizeof(line), f) != NULL) { |
| 88 | /* Going to have issues if we didn't get the full line */ |
| 89 | if (line[strlen(line) - 1] != '\n') |
| 90 | break; |
| 91 | |
| 92 | if (is_pnor_part(line)) { |
| 93 | pos = strchr(line, ':'); |
| 94 | if (!pos) |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | fclose(f); |
| 99 | |
| 100 | if (pos) { |
| 101 | *pos = '\0'; |
| 102 | if (asprintf(&ret, "/dev/%s", line) == -1) |
| 103 | ret = NULL; |
| 104 | } |
| 105 | |
| 106 | return ret; |
| 107 | } |