2 * Copyright (c) 2013-2018, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include "pt_section.h"
30 #include "pt_section_file.h"
38 static int fmap_init(struct pt_sec_file_mapping *mapping)
43 memset(mapping, 0, sizeof(*mapping));
45 #if defined(FEATURE_THREADS)
49 errcode = mtx_init(&mapping->lock, mtx_plain);
50 if (errcode != thrd_success)
53 #endif /* defined(FEATURE_THREADS) */
58 static void fmap_fini(struct pt_sec_file_mapping *mapping)
63 fclose(mapping->file);
65 #if defined(FEATURE_THREADS)
67 mtx_destroy(&mapping->lock);
69 #endif /* defined(FEATURE_THREADS) */
72 static int fmap_lock(struct pt_sec_file_mapping *mapping)
77 #if defined(FEATURE_THREADS)
81 errcode = mtx_lock(&mapping->lock);
82 if (errcode != thrd_success)
85 #endif /* defined(FEATURE_THREADS) */
90 static int fmap_unlock(struct pt_sec_file_mapping *mapping)
95 #if defined(FEATURE_THREADS)
99 errcode = mtx_unlock(&mapping->lock);
100 if (errcode != thrd_success)
101 return -pte_bad_lock;
103 #endif /* defined(FEATURE_THREADS) */
108 int pt_sec_file_map(struct pt_section *section, FILE *file)
110 struct pt_sec_file_mapping *mapping;
111 uint64_t offset, size;
112 long begin, end, fsize;
116 return -pte_internal;
118 mapping = section->mapping;
120 return -pte_internal;
122 offset = section->offset;
123 size = section->size;
125 begin = (long) offset;
126 end = begin + (long) size;
128 /* Check for overflows. */
129 if ((uint64_t) begin != offset)
130 return -pte_bad_image;
132 if ((uint64_t) end != (offset + size))
133 return -pte_bad_image;
136 return -pte_bad_image;
138 /* Validate that the section lies within the file. */
139 errcode = fseek(file, 0, SEEK_END);
141 return -pte_bad_image;
145 return -pte_bad_image;
148 return -pte_bad_image;
150 mapping = malloc(sizeof(*mapping));
154 errcode = fmap_init(mapping);
158 mapping->file = file;
159 mapping->begin = begin;
162 section->mapping = mapping;
163 section->unmap = pt_sec_file_unmap;
164 section->read = pt_sec_file_read;
165 section->memsize = pt_sec_file_memsize;
174 int pt_sec_file_unmap(struct pt_section *section)
176 struct pt_sec_file_mapping *mapping;
179 return -pte_internal;
181 mapping = section->mapping;
183 if (!mapping || !section->unmap || !section->read || !section->memsize)
184 return -pte_internal;
186 section->mapping = NULL;
187 section->unmap = NULL;
188 section->read = NULL;
189 section->memsize = NULL;
197 int pt_sec_file_read(const struct pt_section *section, uint8_t *buffer,
198 uint16_t size, uint64_t offset)
200 struct pt_sec_file_mapping *mapping;
206 if (!buffer || !section)
207 return -pte_internal;
209 mapping = section->mapping;
211 return -pte_internal;
213 file = mapping->file;
215 /* We already checked in pt_section_read() that the requested memory
216 * lies within the section's boundaries.
218 * And we checked that the file covers the entire section in
219 * pt_sec_file_map(). There's no need to check for overflows, again.
221 begin = mapping->begin + (long) offset;
223 errcode = fmap_lock(mapping);
227 errcode = fseek(file, begin, SEEK_SET);
231 read = fread(buffer, 1, size, file);
233 errcode = fmap_unlock(mapping);
240 (void) fmap_unlock(mapping);
244 int pt_sec_file_memsize(const struct pt_section *section, uint64_t *size)
246 if (!section || !size)
247 return -pte_internal;
249 if (!section->mapping)
250 return -pte_internal;