search.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2013, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * Alternatively, this software may be distributed under the terms of the
  32. * GNU General Public License ("GPL") version 2 as published by the Free
  33. * Software Foundation.
  34. *
  35. * This is ported from the flashmap utility: http://flashmap.googlecode.com
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "flash.h"
  41. #include "search.h"
  42. /* Ceil a number to the minimum power of 2 value. For example,
  43. * ceiling(2) = 2
  44. * ceiling(5) = 8
  45. * ceiling(254K) = 256K
  46. *
  47. * Return -1 if the input value is invalid..
  48. */
  49. static long int ceiling(long int v) {
  50. int shiftwidth;
  51. if (v <= 0) return -1;
  52. /* it is power of 2. */
  53. if (!(v & (v - 1))) return v;
  54. /* pollute all bits below MSB to 1. */
  55. for (shiftwidth = (sizeof(v) * CHAR_BIT) / 2;
  56. shiftwidth > 0;
  57. shiftwidth /= 2) {
  58. v = v | (v >> shiftwidth);
  59. }
  60. return v + 1;
  61. }
  62. int search_find_next(struct search_info *search, off_t *offsetp)
  63. {
  64. long int flash_size;
  65. int ret;
  66. flash_size = search->flash->total_size * 1024;
  67. switch (search->state) {
  68. case SEARCH_STATE_START:
  69. search->ceiling_size = ceiling(flash_size);
  70. search->state = SEARCH_STATE_USE_HANDLER;
  71. search->stride = search->ceiling_size / 2;
  72. search->offset = search->ceiling_size - search->stride;
  73. /* no break */
  74. case SEARCH_STATE_USE_HANDLER:
  75. search->state = SEARCH_STATE_BINARY_SEARCH;
  76. search->offset = search->ceiling_size - search->stride;
  77. if (search->handler) {
  78. ret = search->handler(search, offsetp);
  79. if (!ret && *offsetp < flash_size - search->min_size &&
  80. *offsetp >= 0)
  81. return 0;
  82. }
  83. /* no break */
  84. case SEARCH_STATE_BINARY_SEARCH:
  85. /*
  86. * For efficient operation, we start with the largest stride
  87. * possible and then decrease the stride on each iteration. We
  88. * will check for a remainder when modding the offset with the
  89. * previous stride. This makes it so that each offset is only
  90. * checked once.
  91. *
  92. * At some point, programmer transaction overhead becomes
  93. * greater than simply copying everything into RAM and
  94. * checking one byte at a time. At some arbitrary point, we'll
  95. * stop being clever and use brute force instead by copying
  96. * the while ROM into RAM and searching one byte at a time.
  97. *
  98. * In practice, the flash map is usually stored in a
  99. * write-protected section of flash which is often at the top
  100. * of ROM where the boot vector on x86 resides. Because of
  101. * this, we will search from top to bottom.
  102. *
  103. * We assume we can always return at least one offset here.
  104. */
  105. *offsetp = search->offset;
  106. /*
  107. * OK, now what offset should we return next? This loop skips
  108. * any offsets that were already checked by larger strides.
  109. */
  110. do {
  111. search->offset -= search->stride;
  112. } while (search->offset % (search->stride * 2) == 0);
  113. /* Move to next stride if necessary */
  114. if (search->offset < 0) {
  115. search->stride /= 2;
  116. search->offset = search->ceiling_size - search->stride;
  117. while (search->offset > flash_size - search->min_size)
  118. search->offset -= search->stride;
  119. if (search->stride < 16) {
  120. search->state = SEARCH_STATE_FULL_SEARCH;
  121. search->offset = flash_size - 1;
  122. search->image = malloc(flash_size);
  123. if (!search->image) {
  124. msg_gdbg("%s: failed to allocate %ld "
  125. "bytes for search->image",
  126. __func__, flash_size);
  127. return -1;
  128. }
  129. if (read_flash(search->flash, search->image,
  130. 0, flash_size)) {
  131. msg_gdbg("[L%d] failed to read flash contents\n",
  132. __LINE__);
  133. return -1;
  134. }
  135. msg_gdbg("using brute force method to find fmap\n");
  136. }
  137. }
  138. return 0;
  139. case SEARCH_STATE_FULL_SEARCH:
  140. /*
  141. * brute force
  142. * We have read the entire ROM above, so the caller will be
  143. * able to use search->image to access it.
  144. *
  145. * FIXME: This results in the entire ROM being read twice --
  146. * once here and again in doit(). The performance penalty
  147. * needs to be dealt with before going upstream.
  148. */
  149. do {
  150. *offsetp = search->offset--;
  151. } while (*offsetp > flash_size - search->min_size);
  152. if (search->offset < 0)
  153. search->state = SEARCH_STATE_DONE;
  154. return 0;
  155. case SEARCH_STATE_DONE:
  156. break;
  157. }
  158. /* Give up, it is not there */
  159. return -1;
  160. }
  161. void search_init(struct search_info *search, struct flashctx *flash,
  162. int min_size)
  163. {
  164. memset(search, '\0', sizeof(*search));
  165. search->flash = flash;
  166. search->min_size = min_size;
  167. }
  168. void search_free(struct search_info *search)
  169. {
  170. if (search->image)
  171. free(search->image);
  172. }