blob: c065d8ee357af456dcb55af8d6e6e422bddea35d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070016
17#include "image.h"
18
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/object_array.h"
20#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "utils.h"
23
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024namespace art {
25
Ian Rogers13735952014-10-08 12:43:28 -070026const uint8_t ImageHeader::kImageMagic[] = { 'a', 'r', 't', '\n' };
27const uint8_t ImageHeader::kImageVersion[] = { '0', '1', '0', '\0' };
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029ImageHeader::ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070030 uint32_t image_size,
31 uint32_t image_bitmap_offset,
32 uint32_t image_bitmap_size,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 uint32_t image_roots,
34 uint32_t oat_checksum,
35 uint32_t oat_file_begin,
36 uint32_t oat_data_begin,
37 uint32_t oat_data_end,
38 uint32_t oat_file_end)
39 : image_begin_(image_begin),
Mathieu Chartier31e89252013-08-28 11:29:12 -070040 image_size_(image_size),
41 image_bitmap_offset_(image_bitmap_offset),
42 image_bitmap_size_(image_bitmap_size),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 oat_checksum_(oat_checksum),
44 oat_file_begin_(oat_file_begin),
45 oat_data_begin_(oat_data_begin),
46 oat_data_end_(oat_data_end),
47 oat_file_end_(oat_file_end),
Alex Light53cb16b2014-06-12 11:26:29 -070048 patch_delta_(0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 image_roots_(image_roots) {
50 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
51 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
52 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
53 CHECK_LT(image_begin, image_roots);
54 CHECK_LT(image_roots, oat_file_begin);
55 CHECK_LE(oat_file_begin, oat_data_begin);
56 CHECK_LT(oat_data_begin, oat_data_end);
57 CHECK_LE(oat_data_end, oat_file_end);
58 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
59 memcpy(version_, kImageVersion, sizeof(kImageVersion));
60}
61
Alex Light53cb16b2014-06-12 11:26:29 -070062void ImageHeader::RelocateImage(off_t delta) {
63 CHECK_ALIGNED(delta, kPageSize) << " patch delta must be page aligned";
64 image_begin_ += delta;
65 oat_file_begin_ += delta;
66 oat_data_begin_ += delta;
67 oat_data_end_ += delta;
68 oat_file_end_ += delta;
69 image_roots_ += delta;
70 patch_delta_ += delta;
71}
72
Brian Carlstrom68708f52013-09-03 14:15:31 -070073bool ImageHeader::IsValid() const {
74 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) {
75 return false;
76 }
77 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) {
78 return false;
79 }
Alex Light53cb16b2014-06-12 11:26:29 -070080 // Unsigned so wraparound is well defined.
81 if (image_begin_ >= image_begin_ + image_size_) {
82 return false;
83 }
84 if (oat_file_begin_ > oat_file_end_) {
85 return false;
86 }
87 if (oat_data_begin_ > oat_data_end_) {
88 return false;
89 }
90 if (oat_file_begin_ >= oat_data_begin_) {
91 return false;
92 }
93 if (image_roots_ <= image_begin_ || oat_file_begin_ <= image_roots_) {
94 return false;
95 }
96 if (!IsAligned<kPageSize>(patch_delta_)) {
97 return false;
98 }
Brian Carlstrom68708f52013-09-03 14:15:31 -070099 return true;
100}
101
102const char* ImageHeader::GetMagic() const {
103 CHECK(IsValid());
104 return reinterpret_cast<const char*>(magic_);
105}
106
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
108 return GetImageRoots()->Get(image_root);
109}
110
111mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
112 return reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
113}
114
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700115} // namespace art