dump_kernel_config_lib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright 2012 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Exports the kernel commandline from a given partition/image.
  6. */
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/mman.h>
  11. #include <sys/stat.h>
  12. #include <sys/sysmacros.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include "host_common.h"
  16. #include "kernel_blob.h"
  17. #include "vb2_struct.h"
  18. #include "vboot_api.h"
  19. #include "vboot_host.h"
  20. #ifdef USE_MTD
  21. #include <linux/major.h>
  22. #include <mtd/mtd-user.h>
  23. #include <mtdutils.h>
  24. #endif
  25. typedef ssize_t (*ReadFullyFn)(void *ctx, void *buf, size_t count);
  26. static ssize_t ReadFullyWithRead(void *ctx, void *buf, size_t count)
  27. {
  28. ssize_t nr_read = 0;
  29. int fd = *((int*)ctx);
  30. while (nr_read < count) {
  31. ssize_t to_read = count - nr_read;
  32. ssize_t chunk = read(fd, buf + nr_read, to_read);
  33. if (chunk < 0) {
  34. return -1;
  35. } else if (chunk == 0) {
  36. break;
  37. }
  38. nr_read += chunk;
  39. }
  40. return nr_read;
  41. }
  42. #ifdef USE_MTD
  43. static ssize_t ReadFullyWithMtdRead(void *ctx, void *buf, size_t count)
  44. {
  45. MtdReadContext *mtd_ctx = (MtdReadContext*)ctx;
  46. return mtd_read_data(mtd_ctx, buf, count);
  47. }
  48. #endif
  49. /* Skip the stream by calling |read_fn| many times. Return 0 on success. */
  50. static int SkipWithRead(void *ctx, ReadFullyFn read_fn, size_t count)
  51. {
  52. char buf[1024];
  53. ssize_t nr_skipped = 0;
  54. while (nr_skipped < count) {
  55. ssize_t to_read = count - nr_skipped;
  56. if (to_read > sizeof(buf)) {
  57. to_read = sizeof(buf);
  58. }
  59. if (read_fn(ctx, buf, to_read) != to_read) {
  60. return -1;
  61. }
  62. nr_skipped += to_read;
  63. }
  64. return 0;
  65. }
  66. static char *FindKernelConfigFromStream(void *ctx, ReadFullyFn read_fn,
  67. uint64_t kernel_body_load_address)
  68. {
  69. struct vb2_keyblock keyblock;
  70. struct vb2_kernel_preamble preamble;
  71. uint32_t now = 0;
  72. uint32_t offset = 0;
  73. /* Skip the key block */
  74. if (read_fn(ctx, &keyblock, sizeof(keyblock)) != sizeof(keyblock)) {
  75. VbExError("not enough data to fill keyblock header\n");
  76. return NULL;
  77. }
  78. ssize_t to_skip = keyblock.keyblock_size - sizeof(keyblock);
  79. if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
  80. VbExError("keyblock_size advances past the end of the blob\n");
  81. return NULL;
  82. }
  83. now += keyblock.keyblock_size;
  84. /* Open up the preamble */
  85. if (read_fn(ctx, &preamble, sizeof(preamble)) != sizeof(preamble)) {
  86. VbExError("not enough data to fill preamble\n");
  87. return NULL;
  88. }
  89. to_skip = preamble.preamble_size - sizeof(preamble);
  90. if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
  91. VbExError("preamble_size advances past the end of the blob\n");
  92. return NULL;
  93. }
  94. now += preamble.preamble_size;
  95. /* Read body_load_address from preamble if no
  96. * kernel_body_load_address */
  97. if (kernel_body_load_address == USE_PREAMBLE_LOAD_ADDR)
  98. kernel_body_load_address = preamble.body_load_address;
  99. /* The x86 kernels have a pointer to the kernel commandline in the
  100. * zeropage table, but that's irrelevant for ARM. Both types keep the
  101. * config blob in the same place, so just go find it. */
  102. offset = preamble.bootloader_address -
  103. (kernel_body_load_address + CROS_PARAMS_SIZE +
  104. CROS_CONFIG_SIZE) + now;
  105. to_skip = offset - now;
  106. if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
  107. VbExError("params are outside of the memory blob: %x\n",
  108. offset);
  109. return NULL;
  110. }
  111. char *ret = malloc(CROS_CONFIG_SIZE);
  112. if (!ret) {
  113. VbExError("No memory\n");
  114. return NULL;
  115. }
  116. if (read_fn(ctx, ret, CROS_CONFIG_SIZE) != CROS_CONFIG_SIZE) {
  117. VbExError("Cannot read kernel config\n");
  118. free(ret);
  119. ret = NULL;
  120. }
  121. return ret;
  122. }
  123. char *FindKernelConfig(const char *infile, uint64_t kernel_body_load_address)
  124. {
  125. char *newstr = NULL;
  126. int fd = open(infile, O_RDONLY | O_CLOEXEC | O_LARGEFILE);
  127. if (fd < 0) {
  128. VbExError("Cannot open %s\n", infile);
  129. return NULL;
  130. }
  131. void *ctx = &fd;
  132. ReadFullyFn read_fn = ReadFullyWithRead;
  133. #ifdef USE_MTD
  134. struct stat stat_buf;
  135. if (fstat(fd, &stat_buf)) {
  136. VbExError("Cannot stat %s\n", infile);
  137. return NULL;
  138. }
  139. int is_mtd = (major(stat_buf.st_rdev) == MTD_CHAR_MAJOR);
  140. if (is_mtd) {
  141. ctx = mtd_read_descriptor(fd, infile);
  142. if (!ctx) {
  143. VbExError("Cannot read from MTD device %s\n", infile);
  144. return NULL;
  145. }
  146. read_fn = ReadFullyWithMtdRead;
  147. }
  148. #endif
  149. newstr = FindKernelConfigFromStream(ctx, read_fn,
  150. kernel_body_load_address);
  151. #ifdef USE_MTD
  152. if (is_mtd) {
  153. mtd_read_close(ctx);
  154. }
  155. #endif
  156. close(fd);
  157. return newstr;
  158. }