rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <lib/klog.h> |
| 24 | |
| 25 | #include <err.h> |
| 26 | #include <debug.h> |
| 27 | #include <assert.h> |
| 28 | #include <trace.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <platform.h> |
| 33 | #include <lib/cksum.h> |
| 34 | |
| 35 | #define LOCAL_TRACE 0 |
| 36 | |
| 37 | #ifndef MAX_KLOG_SIZE |
| 38 | #define MAX_KLOG_SIZE (32*1024) |
| 39 | #endif |
| 40 | |
| 41 | #define KLOG_BUFFER_HEADER_MAGIC 'KLGB' |
| 42 | |
| 43 | struct klog_buffer_header { |
| 44 | uint32_t magic; |
| 45 | uint32_t header_crc32; |
| 46 | uint32_t log_count; |
| 47 | uint32_t current_log; |
| 48 | uint32_t total_size; |
| 49 | }; |
| 50 | |
| 51 | #define KLOG_HEADER_MAGIC 'KLOG' |
| 52 | |
| 53 | struct klog_header { |
| 54 | uint32_t magic; |
| 55 | uint32_t size; |
| 56 | uint32_t head; |
| 57 | uint32_t tail; |
| 58 | uint32_t data_checksum; |
| 59 | uint8_t data[0]; |
| 60 | }; |
| 61 | |
| 62 | /* current klog buffer */ |
| 63 | static struct klog_buffer_header *klog_buf; |
| 64 | |
| 65 | /* current klog */ |
| 66 | static struct klog_header *klog; |
| 67 | |
| 68 | static struct klog_header *find_nth_log(uint log) |
| 69 | { |
| 70 | DEBUG_ASSERT(klog_buf); |
| 71 | DEBUG_ASSERT(klog_buf->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 72 | DEBUG_ASSERT(log < klog_buf->log_count); |
| 73 | |
| 74 | struct klog_header *k = (struct klog_header *)(klog_buf + 1); |
| 75 | while (log > 0) { |
| 76 | DEBUG_ASSERT(k->magic == KLOG_HEADER_MAGIC); |
| 77 | |
| 78 | uint8_t *ptr = (uint8_t *)k->data; |
| 79 | ptr += k->size; |
| 80 | k = (struct klog_header *)ptr; |
| 81 | |
| 82 | log--; |
| 83 | } |
| 84 | |
| 85 | return k; |
| 86 | } |
| 87 | |
| 88 | static uint32_t get_checksum_klog_buffer_header(const struct klog_buffer_header *kb) |
| 89 | { |
| 90 | DEBUG_ASSERT(kb); |
| 91 | DEBUG_ASSERT(kb->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 92 | |
| 93 | return crc32(0, (const void *)(&kb->header_crc32 + 1), sizeof(*kb) - 8); |
| 94 | } |
| 95 | |
| 96 | static uint32_t get_checksum_klog_data(const struct klog_header *k) |
| 97 | { |
| 98 | DEBUG_ASSERT(k); |
| 99 | DEBUG_ASSERT(k->magic == KLOG_HEADER_MAGIC); |
| 100 | |
| 101 | uint32_t sum = 0; |
| 102 | for (uint i = 0; i < k->size; i++) { |
| 103 | sum += k->data[i]; |
| 104 | } |
| 105 | |
| 106 | return sum; |
| 107 | } |
| 108 | |
| 109 | static void checksum_klog_buffer_header(struct klog_buffer_header *kb) |
| 110 | { |
| 111 | DEBUG_ASSERT(kb); |
| 112 | DEBUG_ASSERT(kb->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 113 | |
| 114 | kb->header_crc32 = get_checksum_klog_buffer_header(kb); |
| 115 | } |
| 116 | |
| 117 | static void checksum_klog_data(struct klog_header *k) |
| 118 | { |
| 119 | DEBUG_ASSERT(k); |
| 120 | DEBUG_ASSERT(k->magic == KLOG_HEADER_MAGIC); |
| 121 | |
| 122 | k->data_checksum = get_checksum_klog_data(k); |
| 123 | } |
| 124 | |
| 125 | void klog_init(void) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | status_t klog_create(void *_ptr, size_t len, uint count) |
| 130 | { |
| 131 | uint8_t *ptr = _ptr; |
| 132 | LTRACEF("ptr %p len %zu count %u\n", ptr, len, count); |
| 133 | |
| 134 | /* check args */ |
| 135 | if (!ptr) |
| 136 | return ERR_INVALID_ARGS; |
| 137 | if (count == 0) |
| 138 | return ERR_INVALID_ARGS; |
| 139 | |
| 140 | /* check that the size is big enough */ |
| 141 | if (len < (sizeof(struct klog_buffer_header) + sizeof(struct klog_header) * count + 4 * count)) |
| 142 | return ERR_INVALID_ARGS; |
| 143 | |
| 144 | /* set up the buffer header */ |
| 145 | klog_buf = (struct klog_buffer_header *)ptr; |
| 146 | klog_buf->magic = KLOG_BUFFER_HEADER_MAGIC; |
| 147 | klog_buf->log_count = count; |
| 148 | klog_buf->current_log = 0; |
| 149 | klog_buf->total_size = len; |
| 150 | checksum_klog_buffer_header(klog_buf); |
| 151 | ptr += sizeof(struct klog_buffer_header); |
| 152 | |
| 153 | /* set up each buffer */ |
| 154 | uint bufsize = len - sizeof(struct klog_buffer_header) - sizeof(struct klog_header) * count; |
| 155 | bufsize /= count; |
| 156 | bufsize = ROUNDDOWN(bufsize, 4); |
| 157 | while (count > 0) { |
| 158 | klog = (struct klog_header *)ptr; |
| 159 | klog->magic = KLOG_HEADER_MAGIC; |
| 160 | klog->size = bufsize; |
| 161 | klog->head = 0; |
| 162 | klog->tail = 0; |
| 163 | klog->data_checksum = 0; |
| 164 | memset(klog + 1, 0, bufsize); |
| 165 | checksum_klog_data(klog); |
| 166 | ptr += sizeof(struct klog_header) + bufsize; |
| 167 | count--; |
| 168 | } |
| 169 | |
| 170 | klog_set_current_buffer(0); |
| 171 | |
| 172 | DEBUG_ASSERT(klog_buf); |
| 173 | DEBUG_ASSERT(klog); |
| 174 | |
| 175 | return NO_ERROR; |
| 176 | } |
| 177 | |
| 178 | ssize_t klog_recover(void *_ptr) |
| 179 | { |
| 180 | uint8_t *ptr = _ptr; |
| 181 | LTRACEF("ptr %p\n", ptr); |
| 182 | |
| 183 | if (!ptr) |
| 184 | return ERR_INVALID_ARGS; |
| 185 | |
| 186 | /* look for header at pointer */ |
| 187 | struct klog_buffer_header *kbuf = (struct klog_buffer_header *)ptr; |
| 188 | if (kbuf->magic != KLOG_BUFFER_HEADER_MAGIC) |
| 189 | return ERR_NOT_FOUND; |
| 190 | uint32_t crc = get_checksum_klog_buffer_header(kbuf); |
| 191 | if (crc != kbuf->header_crc32) |
| 192 | return ERR_NOT_FOUND; |
| 193 | |
| 194 | /* some sanity checks */ |
| 195 | if (kbuf->total_size > MAX_KLOG_SIZE) |
| 196 | return ERR_NOT_FOUND; |
| 197 | if (kbuf->current_log >= kbuf->log_count) |
| 198 | return ERR_NOT_FOUND; |
| 199 | |
| 200 | /* walk the list of klogs, validating */ |
| 201 | ptr += sizeof(struct klog_buffer_header); |
| 202 | for (uint i = 0; i < kbuf->log_count; i++) { |
| 203 | struct klog_header *k = (struct klog_header *)ptr; |
| 204 | |
| 205 | /* validate the individual klog */ |
| 206 | if (k->magic != KLOG_HEADER_MAGIC) |
| 207 | return ERR_NOT_FOUND; |
| 208 | |
| 209 | /* validate some fields */ |
| 210 | if ((k->size > 0) && (k->size & 3)) |
| 211 | return ERR_NOT_FOUND; |
| 212 | if (k->size > MAX_KLOG_SIZE) |
| 213 | return ERR_NOT_FOUND; |
| 214 | if (k->head >= k->size) |
| 215 | return ERR_NOT_FOUND; |
| 216 | if (k->tail >= k->size) |
| 217 | return ERR_NOT_FOUND; |
| 218 | |
| 219 | /* data checksum */ |
| 220 | if (k->data_checksum) { |
| 221 | crc = get_checksum_klog_data(k); |
| 222 | if (crc != k->data_checksum) |
| 223 | return ERR_NOT_FOUND; |
| 224 | } |
| 225 | |
| 226 | ptr += sizeof(struct klog_header) + k->size; |
| 227 | } |
| 228 | |
| 229 | /* everything checks out */ |
| 230 | klog_buf = kbuf; |
| 231 | klog_set_current_buffer(klog_buf->current_log); |
| 232 | |
| 233 | LTRACEF("found buffer at %p, current log %u (%p) head %u tail %u size %u\n", |
| 234 | klog_buf, klog_buf->current_log, klog, klog->head, klog->tail, klog->size); |
| 235 | |
| 236 | return NO_ERROR; |
| 237 | } |
| 238 | |
| 239 | uint klog_buffer_count(void) |
| 240 | { |
| 241 | if (!klog_buf) |
| 242 | return 0; |
| 243 | |
| 244 | DEBUG_ASSERT(klog_buf); |
| 245 | DEBUG_ASSERT(klog_buf->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 246 | |
| 247 | return klog_buf->log_count; |
| 248 | } |
| 249 | |
| 250 | uint klog_current_buffer(void) |
| 251 | { |
| 252 | if (!klog_buf) |
| 253 | return 0; |
| 254 | |
| 255 | DEBUG_ASSERT(klog_buf); |
| 256 | DEBUG_ASSERT(klog_buf->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 257 | |
| 258 | return klog_buf->current_log; |
| 259 | } |
| 260 | |
| 261 | status_t klog_set_current_buffer(uint buffer) |
| 262 | { |
| 263 | if (!klog_buf) |
| 264 | return ERR_NOT_FOUND; |
| 265 | |
| 266 | DEBUG_ASSERT(klog_buf); |
| 267 | DEBUG_ASSERT(klog_buf->magic == KLOG_BUFFER_HEADER_MAGIC); |
| 268 | |
| 269 | if (buffer >= klog_buf->log_count) |
| 270 | return ERR_INVALID_ARGS; |
| 271 | |
| 272 | /* find the nth buffer */ |
| 273 | klog = find_nth_log(buffer); |
| 274 | |
| 275 | /* update the klog buffer header */ |
| 276 | if (buffer != klog_buf->current_log) { |
| 277 | klog_buf->current_log = buffer; |
| 278 | checksum_klog_buffer_header(klog_buf); |
| 279 | } |
| 280 | |
| 281 | return NO_ERROR; |
| 282 | } |
| 283 | |
| 284 | #include <arch/ops.h> |
| 285 | |
| 286 | ssize_t klog_read(char *buf, size_t len, int buf_id) |
| 287 | { |
| 288 | size_t offset = 0; |
| 289 | size_t tmp_len; |
| 290 | iovec_t vec[2]; |
| 291 | LTRACEF("read (len %zu, buf %u)\n", len, buf_id); |
| 292 | |
| 293 | DEBUG_ASSERT(klog); |
| 294 | DEBUG_ASSERT(klog->magic == KLOG_HEADER_MAGIC); |
| 295 | |
| 296 | /* If a klog wraps around at the end then it becomes two iovecs with |
| 297 | * tail being the start of 1 and head being the end of 0. This means we |
| 298 | * need to check where we are in the overall klog to properly determine |
| 299 | * which iovec we want to read from */ |
| 300 | int vec_cnt = klog_get_buffer(buf_id, vec); |
| 301 | if (vec_cnt < 1) |
| 302 | return vec_cnt; |
| 303 | |
| 304 | tmp_len = MIN(len, vec[0].iov_len); |
| 305 | memcpy(buf, (const char *)vec[0].iov_base, tmp_len); |
| 306 | offset += tmp_len; |
| 307 | len -= tmp_len; |
| 308 | |
| 309 | if (len != 0 && vec_cnt > 1) { |
| 310 | tmp_len = MIN(len, vec[1].iov_len); |
| 311 | memcpy(buf + offset, (const char *)vec[1].iov_base, tmp_len); |
| 312 | offset += tmp_len; |
| 313 | } |
| 314 | |
| 315 | /* Since iovecs are generated by get_buffer we only need to update the tail pointer */ |
| 316 | klog->tail += offset; |
| 317 | if (klog->tail >= klog->size) |
| 318 | klog->tail -= klog->size; |
| 319 | |
| 320 | return offset; |
| 321 | } |
| 322 | |
| 323 | char klog_getc(int buf_id) |
| 324 | { |
| 325 | char c = '\0'; |
| 326 | int err = klog_read(&c, 1, buf_id); |
| 327 | |
| 328 | return (err < 0) ? err : c; |
| 329 | } |
| 330 | |
| 331 | char klog_getchar(void) |
| 332 | { |
| 333 | return klog_getc(-1); |
| 334 | } |
| 335 | |
| 336 | /* Returns whether the currently selected klog contains data */ |
| 337 | bool klog_has_data(void) |
| 338 | { |
| 339 | DEBUG_ASSERT(klog); |
| 340 | |
| 341 | return (klog->head != klog->tail); |
| 342 | } |
| 343 | |
| 344 | static size_t klog_puts_len(const char *str, size_t len) |
| 345 | { |
| 346 | LTRACEF("puts '%s'\n", str); |
| 347 | |
| 348 | DEBUG_ASSERT(klog); |
| 349 | DEBUG_ASSERT(klog->magic == KLOG_HEADER_MAGIC); |
| 350 | |
| 351 | LTRACEF("before write head %u tail %u size %u\n", klog->head, klog->tail, klog->size); |
| 352 | uint32_t deltasum = 0; |
| 353 | size_t count = 0; |
| 354 | while (count < len && *str) { |
| 355 | /* compute the delta checksum */ |
| 356 | deltasum += *str - klog->data[klog->head]; |
| 357 | |
| 358 | /* store the data */ |
| 359 | klog->data[klog->head] = *str; |
| 360 | |
| 361 | /* bump the head */ |
| 362 | uint newhead = klog->head + 1; |
| 363 | if (newhead >= klog->size) |
| 364 | newhead -= klog->size; |
| 365 | DEBUG_ASSERT(newhead < klog->size); |
| 366 | |
| 367 | /* bump the tail if the head collided with it */ |
| 368 | if (klog->tail == newhead) { |
| 369 | uint newtail = klog->tail + 1; |
| 370 | if (newtail >= klog->size) |
| 371 | newtail -= klog->size; |
| 372 | DEBUG_ASSERT(newtail < klog->size); |
| 373 | klog->tail = newtail; |
| 374 | } |
| 375 | |
| 376 | /* writeback the head */ |
| 377 | klog->head = newhead; |
| 378 | |
| 379 | str++; |
| 380 | count++; |
| 381 | } |
| 382 | LTRACEF("after write head %u tail %u\n", klog->head, klog->tail); |
| 383 | |
| 384 | klog->data_checksum += deltasum; |
| 385 | |
| 386 | LTRACEF("kputs len %u\n", count); |
| 387 | |
| 388 | return count; |
| 389 | } |
| 390 | |
| 391 | void klog_putchar(char c) |
| 392 | { |
| 393 | klog_puts_len(&c, 1); |
| 394 | } |
| 395 | |
| 396 | void klog_puts(const char *str) |
| 397 | { |
| 398 | if (!klog_buf) |
| 399 | return; |
| 400 | |
| 401 | klog_puts_len(str, SIZE_MAX); |
| 402 | } |
| 403 | |
| 404 | static int _klog_output_func(const char *str, size_t len, void *state) |
| 405 | { |
| 406 | return klog_puts_len(str, len); |
| 407 | } |
| 408 | |
| 409 | void klog_printf(const char *fmt, ...) |
| 410 | { |
| 411 | if (!klog_buf) |
| 412 | return; |
| 413 | |
| 414 | va_list ap; |
| 415 | va_start(ap, fmt); |
| 416 | _printf_engine(&_klog_output_func, NULL, fmt, ap); |
| 417 | va_end(ap); |
| 418 | } |
| 419 | |
| 420 | void klog_vprintf(const char *fmt, va_list ap) |
| 421 | { |
| 422 | if (!klog_buf) |
| 423 | return; |
| 424 | |
| 425 | _printf_engine(&_klog_output_func, NULL, fmt, ap); |
| 426 | } |
| 427 | |
| 428 | int klog_get_buffer(int buffer, iovec_t *vec) |
| 429 | { |
| 430 | if (!klog_buf) |
| 431 | return 0; |
| 432 | if (!vec) |
| 433 | return ERR_INVALID_ARGS; |
| 434 | if (buffer >= 0 && (uint)buffer >= klog_buf->log_count) |
| 435 | return ERR_INVALID_ARGS; |
| 436 | |
| 437 | struct klog_header *k; |
| 438 | if (buffer < 0) |
| 439 | k = klog; |
| 440 | else |
| 441 | k = find_nth_log(buffer); |
| 442 | |
| 443 | DEBUG_ASSERT(k); |
| 444 | DEBUG_ASSERT(k->magic == KLOG_HEADER_MAGIC); |
| 445 | |
| 446 | vec[0].iov_base = &k->data[k->tail]; |
| 447 | if (k->head == k->tail) { |
| 448 | return 0; |
| 449 | } else if (k->head > k->tail) { |
| 450 | /* single run of data, between tail and head */ |
| 451 | vec[0].iov_len = k->head - k->tail; |
| 452 | |
| 453 | return 1; |
| 454 | } else { |
| 455 | vec[0].iov_len = k->size - k->tail; |
| 456 | |
| 457 | /* two segments */ |
| 458 | vec[1].iov_base = &k->data[0]; |
| 459 | vec[1].iov_len = k->head; |
| 460 | |
| 461 | return 2; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void klog_dump(int buffer) |
| 466 | { |
| 467 | iovec_t vec[2]; |
| 468 | |
| 469 | int err = klog_get_buffer(buffer, vec); |
| 470 | if (err <= 0) |
| 471 | return; |
| 472 | |
| 473 | for (uint i = 0; i < vec[0].iov_len; i++) |
| 474 | putchar(*((const char *)vec[0].iov_base + i)); |
| 475 | if (err > 1) { |
| 476 | for (uint i = 0; i < vec[1].iov_len; i++) |
| 477 | putchar(*((const char *)vec[1].iov_base + i)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | #if WITH_LIB_CONSOLE |
| 482 | |
| 483 | #include <lib/console.h> |
| 484 | |
| 485 | #define KLOG_RETENTION_TEST 0 |
| 486 | #if KLOG_RETENTION_TEST |
| 487 | #include <platform/retention.h> |
| 488 | |
| 489 | _RETENTION_NOCLEAR(static uint8_t klog_test_buf[512]); |
| 490 | #endif |
| 491 | |
| 492 | static int cmd_klog(int argc, const cmd_args *argv) |
| 493 | { |
| 494 | status_t err; |
| 495 | |
| 496 | if (argc < 2) { |
| 497 | notenoughargs: |
| 498 | printf("ERROR not enough arguments\n"); |
| 499 | usage: |
| 500 | printf("usage: %s create <size> <count>\n", argv[0].str); |
| 501 | #if KLOG_RETENTION_TEST |
| 502 | printf("usage: %s createret \n", argv[0].str); |
| 503 | printf("usage: %s recoverret \n", argv[0].str); |
| 504 | #endif |
| 505 | printf("usage: %s getbufcount\n", argv[0].str); |
| 506 | printf("usage: %s getbufnum\n", argv[0].str); |
| 507 | printf("usage: %s getbufptr\n", argv[0].str); |
| 508 | printf("usage: %s setbufnum <num>\n", argv[0].str); |
| 509 | printf("usage: %s puts <string>\n", argv[0].str); |
| 510 | printf("usage: %s read <length> <buffer num>\n", argv[0].str); |
| 511 | printf("usage: %s printftest\n", argv[0].str); |
| 512 | printf("usage: %s dump [buffer num]\n", argv[0].str); |
| 513 | printf("usage: %s vec [buffer num]\n", argv[0].str); |
| 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | if (!strcmp(argv[1].str, "create")) { |
| 518 | if (argc < 4) goto notenoughargs; |
| 519 | |
| 520 | uint size = argv[2].u; |
| 521 | uint count = argv[3].u; |
| 522 | |
| 523 | void *ptr = malloc(size); |
| 524 | if (!ptr) { |
| 525 | printf("error allocating memory for klog\n"); |
| 526 | return -1; |
| 527 | } |
| 528 | err = klog_create(ptr, size, count); |
| 529 | printf("klog_create returns %d\n", err); |
| 530 | if (err < 0) |
| 531 | free(ptr); |
| 532 | #if KLOG_RETENTION_TEST |
| 533 | } else if (!strcmp(argv[1].str, "createret")) { |
| 534 | err = klog_create(klog_test_buf, sizeof(klog_test_buf), 1); |
| 535 | printf("klog_create returns %d\n", err); |
| 536 | } else if (!strcmp(argv[1].str, "recoverret")) { |
| 537 | err = klog_recover(klog_test_buf); |
| 538 | printf("klog_recover returns %d\n", err); |
| 539 | #endif |
| 540 | } else if (!strcmp(argv[1].str, "getbufcount")) { |
| 541 | printf("%d buffers\n", klog_buffer_count()); |
| 542 | } else if (!strcmp(argv[1].str, "getbufnum")) { |
| 543 | printf("%d current buffer\n", klog_current_buffer()); |
| 544 | } else if (!strcmp(argv[1].str, "getbufptr")) { |
| 545 | printf("ptr %p\n", klog); |
| 546 | } else if (!strcmp(argv[1].str, "setbufnum")) { |
| 547 | if (argc < 3) goto notenoughargs; |
| 548 | |
| 549 | err = klog_set_current_buffer(argv[2].u); |
| 550 | printf("klog_set_current_buffer returns %d\n", err); |
| 551 | } else if (!strcmp(argv[1].str, "puts")) { |
| 552 | if (argc < 3) goto notenoughargs; |
| 553 | |
| 554 | klog_puts(argv[2].str); |
| 555 | klog_putchar('\n'); |
| 556 | } else if (!strcmp(argv[1].str, "read")) { |
| 557 | if (argc < 4) goto notenoughargs; |
| 558 | size_t len = argv[2].u; |
| 559 | int buf_id = argv[3].i; |
| 560 | char *buf = malloc(len); |
| 561 | if (!buf) { |
| 562 | printf("error allocating memory for klog read\n"); |
| 563 | return -1; |
| 564 | } |
| 565 | size_t count = klog_read(buf, len, buf_id); |
| 566 | if (count > 0) { |
| 567 | printf("read %zu byte(s): \"", count); |
| 568 | for (size_t i = 0; i < count; i++) |
| 569 | putchar(buf[i]); |
| 570 | putchar('\"'); |
| 571 | putchar('\n'); |
| 572 | } else { |
| 573 | printf("read returned error: %d\n", count); |
| 574 | } |
| 575 | free(buf); |
| 576 | } else if (!strcmp(argv[1].str, "getc")) { |
| 577 | if (argc < 3) goto notenoughargs; |
| 578 | int buf_id = argv[2].i; |
| 579 | printf("read: '%c'\n", klog_getc(buf_id)); |
| 580 | } else if (!strcmp(argv[1].str, "printftest")) { |
| 581 | klog_printf("a plain string\n"); |
| 582 | klog_printf("numbers: %d %d %d %u\n", 1, 2, 3, 99); |
| 583 | klog_printf("strings: '%s' '%s'\n", "a little string", "another one"); |
| 584 | } else if (!strcmp(argv[1].str, "dump")) { |
| 585 | int buffer = -1; |
| 586 | |
| 587 | if (argc >= 3) |
| 588 | buffer = argv[2].u; |
| 589 | |
| 590 | klog_dump(buffer); |
| 591 | } else if (!strcmp(argv[1].str, "vec")) { |
| 592 | int buffer = -1; |
| 593 | |
| 594 | if (argc >= 3) |
| 595 | buffer = argv[2].u; |
| 596 | |
| 597 | iovec_t vec[2]; |
| 598 | memset(vec, 0x99, sizeof(vec)); |
| 599 | int err = klog_get_buffer(buffer, vec); |
| 600 | printf("klog_get_buffer returns %d\n", err); |
| 601 | printf("vec %d: base %p, len %zu\n", 0, vec[0].iov_base, vec[0].iov_len); |
| 602 | printf("vec %d: base %p, len %zu\n", 1, vec[1].iov_base, vec[1].iov_len); |
| 603 | } else { |
| 604 | printf("ERROR unknown command\n"); |
| 605 | goto usage; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | STATIC_COMMAND_START |
| 612 | STATIC_COMMAND("klog", "commands for manipulating klog", &cmd_klog) |
| 613 | STATIC_COMMAND_END(klog); |
| 614 | |
| 615 | #endif // WITH_LIB_CONSOLE |
| 616 | |