search.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef FLASHMAP_LIB_SEARCH_H__
  38. #define FLASHMAP_LIB_SEARCH_H__
  39. /* Our current state in the search process */
  40. enum search_state_t {
  41. SEARCH_STATE_START,
  42. SEARCH_STATE_USE_HANDLER, /* Call handler function */
  43. SEARCH_STATE_BINARY_SEARCH, /* Fast binary search */
  44. SEARCH_STATE_FULL_SEARCH, /* Slow incremental search */
  45. SEARCH_STATE_DONE, /* Search completed */
  46. };
  47. /* Keeps track of the state of our search */
  48. struct search_info {
  49. struct flashctx *flash; /* Flash information */
  50. enum search_state_t state; /* Current state */
  51. long int ceiling_size; /* Lowest power of 2 >= flash size */
  52. long int stride; /* Current binary search stride */
  53. off_t offset; /* Next offset to return */
  54. uint8_t *image; /* Cache of entire flash image */
  55. int min_size; /* Minimum size of data to find */
  56. /*
  57. * Utility wrapper for using external programs to aid in our search.
  58. * @search: Pointer to search information
  59. * @offset: Wrapper will set this if successful
  60. *
  61. * @return 0 if successful, -1 to indicate failure or offset not
  62. * found by utility
  63. */
  64. int (*handler)(struct search_info *search, off_t *offset);
  65. };
  66. /**
  67. * search_find_next() - Find the next offset to check in a search operation
  68. *
  69. * If search->image is not NULL, then it contains the full flash image and
  70. * the caller can use this instead of reading the data again.
  71. *
  72. * @search: Search information, set up by search_init()
  73. * @offsetp: Returns next offset to check
  74. * @return 0 if we have an offset, -1 if we have run out of places to look
  75. */
  76. int search_find_next(struct search_info *search, off_t *offsetp);
  77. /** search_init() - Get ready to start a search
  78. *
  79. * @search: Search information, set up by this function
  80. * @flash: Information about the flash chip
  81. * @min_size: Minimum size of region that we want to find
  82. */
  83. void search_init(struct search_info *search, struct flashctx *flash,
  84. int min_size);
  85. /** search_free() - Free memory allocated by search
  86. *
  87. * @search: Search information, set up by search_init()
  88. */
  89. void search_free(struct search_info *search);
  90. #endif