fmap.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2010, 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_FMAP_H__
  38. #define FLASHMAP_LIB_FMAP_H__
  39. #ifndef _GNU_SOURCE
  40. #define _GNU_SOURCE
  41. #endif
  42. #include <inttypes.h>
  43. #include <fcntl.h>
  44. struct flashctx;
  45. #define FMAP_SIGNATURE "__FMAP__"
  46. #define FMAP_VER_MAJOR 1 /* this header's FMAP minor version */
  47. #define FMAP_VER_MINOR 1 /* this header's FMAP minor version */
  48. #define FMAP_STRLEN 32 /* maximum length for strings, */
  49. /* including null-terminator */
  50. enum fmap_flags {
  51. FMAP_AREA_STATIC = 1 << 0,
  52. FMAP_AREA_COMPRESSED = 1 << 1,
  53. FMAP_AREA_RO = 1 << 2,
  54. };
  55. struct fmap_area {
  56. uint32_t offset; /* offset relative to base */
  57. uint32_t size; /* size in bytes */
  58. uint8_t name[FMAP_STRLEN]; /* descriptive name */
  59. uint16_t flags; /* flags for this area */
  60. } __attribute__((packed));
  61. /* Mapping of volatile and static regions in firmware binary */
  62. struct fmap {
  63. uint64_t signature; /* "__FMAP__" (0x5F5F50414D465F5F) */
  64. uint8_t ver_major; /* major version */
  65. uint8_t ver_minor; /* minor version */
  66. uint64_t base; /* address of the firmware binary */
  67. uint32_t size; /* size of firmware binary in bytes */
  68. uint8_t name[FMAP_STRLEN]; /* name of this firmware binary */
  69. uint16_t nareas; /* number of areas described by
  70. fmap_areas[] below */
  71. struct fmap_area areas[];
  72. } __attribute__((packed));
  73. struct search_info;
  74. /*
  75. * fmap_find - find FMAP signature at offset in an image and copy it to buffer
  76. *
  77. * @flash: flash structure containing read function
  78. * @fmap: pointer to fmap header
  79. * @offset: offset of fmap header in image
  80. * @buf: unallocated buffer to store fmap struct
  81. *
  82. * This function allocates memory which the caller must free. It does no error
  83. * checking. The caller is responsible for verifying that the contents are
  84. * sane.
  85. *
  86. * returns 1 if found, 0 if not found, <0 to indicate failure
  87. */
  88. int fmap_find(struct flashctx *flash, struct fmap *fmap, loff_t offset,
  89. uint8_t **buf);
  90. /* Like fmap_find, but give a memory location to search FMAP. */
  91. struct fmap *fmap_find_in_memory(uint8_t *image, int size);
  92. #endif /* FLASHMAP_LIB_FMAP_H__*/