ramdisk_merge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2018. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stddef.h>
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <lib/ramdisk_merge.h>
  38. #include <lib/zlib.h>
  39. #if _WIN32
  40. #define PROFILING_START(a)
  41. #define PROFILING_END()
  42. #else
  43. #include <platform/mt_gpt.h>
  44. #include <profiling.h>
  45. #endif // _WIN32
  46. #include <memory_layout.h>
  47. #include "ramdisk_merge_private.h"
  48. #include "cpio_header.h"
  49. #include <platform/boot_mode.h>
  50. #include <mblock.h>
  51. /* static function declaration */
  52. static int validate_cpio_archive(const uint8_t *cpio_archive);
  53. static int find_cpio_last_entry(const uint8_t *cpio_archive, const uint32_t cpio_archive_size, uint8_t **last_entry);
  54. static void gzip_uncompressed_size(const uint8_t *gzip_stream, uint32_t gzip_size, uint32_t *uncompressed_size);
  55. static int ramdisk_merge(ramdisk_buf_t *ramdisk1, ramdisk_buf_t *ramdisk2, ramdisk_buf_t *target_ramdisk);
  56. #define RAMDISK_TEMP_BUF_MAX_SIZE (100*1024*1024)
  57. #define RAMDISK_TEMP_BUF_LIMIT (0xc0000000)
  58. /*
  59. * LK ramdisk merge deal with ramdisk create by mkbootfs.
  60. * cpio archive is newc format and always create TRAILER!!! as last entry.
  61. */
  62. /*
  63. https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
  64. The full format of the initramfs buffer is defined by the following
  65. grammar, where:
  66. * is used to indicate "0 or more occurrences of"
  67. (|) indicates alternatives
  68. + indicates concatenation
  69. GZIP() indicates the gzip(1) of the operand
  70. ALGN(n) means padding with null bytes to an n-byte boundary
  71. initramfs := ("\0" | cpio_archive | cpio_gzip_archive)*
  72. cpio_gzip_archive := GZIP(cpio_archive)
  73. cpio_archive := cpio_file* + (<nothing> | cpio_trailer)
  74. cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data
  75. cpio_trailer := ALGN(4) + cpio_header + "TRAILER!!!\0" + ALGN(4)
  76. */
  77. /* recovery_ramdisk_buf point to work buffer head */
  78. int recovery_ramdisk_merge(ramdisk_buf_t *r_ramdisk, ramdisk_buf_t *v_ramdisk, ramdisk_buf_t *target_ramdisk,
  79. uint8_t *work_buf, uint8_t *work_buf_end)
  80. {
  81. int rc;
  82. uint8_t *work_buf_mblock;
  83. uint8_t *work_buf_end_mblock;
  84. uint64_t work_buf_size_mblock;
  85. gzip_uncompressed_size(r_ramdisk->comp_buf, r_ramdisk->comp_size, &r_ramdisk->decomp_max_size);
  86. r_ramdisk->decomp_max_size += UNCOMPRESS_GUARD_BUFFER;
  87. gzip_uncompressed_size(v_ramdisk->comp_buf, v_ramdisk->comp_size, &v_ramdisk->decomp_max_size);
  88. v_ramdisk->decomp_max_size += UNCOMPRESS_GUARD_BUFFER;
  89. work_buf_size_mblock = r_ramdisk->decomp_max_size + v_ramdisk->decomp_max_size;
  90. RM_DEBUG("real merged size:%llu\n", work_buf_size_mblock);
  91. if (work_buf_size_mblock > RAMDISK_TEMP_BUF_MAX_SIZE)
  92. RM_DEBUG("WARNING! work_buf_size_mblock size[%llu] is over %u!!\n", work_buf_size_mblock, RAMDISK_TEMP_BUF_MAX_SIZE);
  93. else
  94. work_buf_size_mblock = RAMDISK_TEMP_BUF_MAX_SIZE;
  95. if (work_buf_size_mblock > LK_RAMDISK_MAX_SIZE)
  96. panic("work_buf_size_mblock size[%llu] is not allowed over %u!!\n", work_buf_size_mblock, LK_RAMDISK_MAX_SIZE);
  97. /* Allocate temp buffer for ramdisk merge */
  98. work_buf_mblock = (uint8_t *)(uint32_t)mblock_reserve_ext(&g_boot_arg->mblock_info,
  99. work_buf_size_mblock, PAGE_SIZE, RAMDISK_TEMP_BUF_LIMIT,
  100. 0, "ramdisk_merge_temp");
  101. if (work_buf_mblock == 0)
  102. panic("work_buf_mblock is not allocated successfully!!");
  103. work_buf_end_mblock = work_buf_mblock + work_buf_size_mblock;
  104. RM_DEBUG("work_buf_mblock:0x%x, work_buf_end_mblock:0x%x\n", (uint32_t)work_buf_mblock, (uint32_t)work_buf_end_mblock);
  105. /* cpio head needs 4 byte alignment */
  106. r_ramdisk->decomp_buf = (uint8_t *)ALIGN((size_t)work_buf_mblock, 4);
  107. v_ramdisk->decomp_buf = (uint8_t *)ALIGN((size_t)(r_ramdisk->decomp_buf + r_ramdisk->decomp_max_size), 4);
  108. if ((v_ramdisk->decomp_buf + v_ramdisk->decomp_max_size) >= work_buf_end_mblock) {
  109. DUMP_RAMDISK_STRUCT(r_ramdisk);
  110. DUMP_RAMDISK_STRUCT(v_ramdisk);
  111. assert(0);
  112. }
  113. rc = ramdisk_merge(r_ramdisk, v_ramdisk, target_ramdisk);
  114. /* Release temp buffer */
  115. mblock_create(&g_boot_arg->mblock_info,
  116. &g_boot_arg->orig_dram_info,
  117. (uint64_t)work_buf_mblock & 0xffffffff, (uint64_t)work_buf_size_mblock & 0xffffffff);
  118. return rc;
  119. }
  120. static void gzip_uncompressed_size(const uint8_t *gzip_stream, uint32_t gzip_size, uint32_t *uncompressed_size)
  121. {
  122. uint8_t *gzip_ptr = (uint8_t *) gzip_stream;
  123. uint32_t size;
  124. /*
  125. * The last 4 byte presents the uncompressed data size modulo 2^32.
  126. * This value is not correct if uncompressed data size is larger than 4GB.
  127. * However, ramdisk should be rather small so we take it as real uncompressed size.
  128. */
  129. gzip_ptr = (gzip_ptr + gzip_size - GZIP_HEADER_UNCOMPRESSED_DATA_SIZE_OFFSET_TAIL);
  130. /*
  131. * gzip_ptr may be not 4 byte alignment. ARM needs to
  132. * access address natural alignment.
  133. */
  134. size = gzip_ptr[0] | gzip_ptr[1] << 8 | gzip_ptr[2] << 16 | gzip_ptr[3] << 24;
  135. /* In case there is a parsing error */
  136. if (size > LK_RAMDISK_MAX_SIZE) {
  137. RM_DEBUG("Uncompressed ramdisk size may be too large! %u\n", size);
  138. assert(0);
  139. }
  140. *uncompressed_size = size;
  141. }
  142. static int ramdisk_merge(ramdisk_buf_t *ramdisk1, ramdisk_buf_t *ramdisk2, ramdisk_buf_t *target_ramdisk)
  143. {
  144. int rc;
  145. uint8_t *tail_without_last_entry;
  146. uint32_t total_cpio_merged_size;
  147. z_stream def_target;
  148. validate_ramdisk_buf(ramdisk1);
  149. /* Decompress ramdisk */
  150. RM_DEBUG("ramdisk1->comp_buf:0x%x, comp_size:%u\n", (uint32_t)ramdisk1->comp_buf, ramdisk1->comp_size);
  151. rc = gunzip(ramdisk1->comp_buf, (unsigned long *)&ramdisk1->comp_size,
  152. ramdisk1->decomp_buf, ramdisk1->decomp_max_size);
  153. assert(rc == 0);
  154. ramdisk1->decomp_size = ramdisk1->comp_size;
  155. RM_DEBUG("ramdisk1 decompress size:%u\n", ramdisk1->decomp_size);
  156. /* Validate cpio format */
  157. rc = validate_cpio_archive(ramdisk1->decomp_buf);
  158. if (rc != 0) {
  159. RM_DEBUG("validate 1st ramdisk fail!\n");
  160. DUMP_RAMDISK_STRUCT(ramdisk1);
  161. assert(0);
  162. }
  163. /* Find ramdisk1 last cpio entry*/
  164. rc = find_cpio_last_entry(ramdisk1->decomp_buf, ramdisk1->decomp_size, &tail_without_last_entry);
  165. if (rc != 0) {
  166. RM_DEBUG("Fail to find cpio last entry! %d\n", rc);
  167. DUMP_RAMDISK_STRUCT(ramdisk1);
  168. assert(!rc);
  169. }
  170. ramdisk2->decomp_buf = tail_without_last_entry;
  171. RM_DEBUG("ramdisk2->comp_buf:0x%x, comp_size:%u\n", (uint32_t)ramdisk2->comp_buf, ramdisk2->comp_size);
  172. validate_ramdisk_buf(ramdisk2);
  173. /* Concatenate ramdisk2 to truncated ramdisk1's tail */
  174. rc = gunzip(ramdisk2->comp_buf, (unsigned long *)&ramdisk2->comp_size,
  175. ramdisk2->decomp_buf, ramdisk2->decomp_max_size);
  176. assert(rc == 0);
  177. ramdisk2->decomp_size = ramdisk2->comp_size;
  178. RM_DEBUG("ramdisk2 decompress size:%u\n", ramdisk2->decomp_size);
  179. /* Validate cpio archive format after concat */
  180. rc = validate_cpio_archive(ramdisk2->decomp_buf);
  181. if (rc != 0) {
  182. RM_DEBUG("validate 2nd ramdisk fail!\n");
  183. DUMP_RAMDISK_STRUCT(ramdisk2);
  184. assert(0);
  185. }
  186. total_cpio_merged_size = (tail_without_last_entry - ramdisk1->decomp_buf) + ramdisk2->decomp_size;
  187. RM_DEBUG("total_cpio_merged_size:%u\n", total_cpio_merged_size);
  188. /* Compress merged cpio archive */
  189. def_target.zalloc = Z_NULL;
  190. def_target.zfree = Z_NULL;
  191. def_target.opaque = Z_NULL;
  192. rc = deflateInit2 (&def_target, COMPRESSION_LEVEL, Z_DEFLATED,
  193. WINDOWBITS | GZIP_ENCODING, 8,
  194. Z_DEFAULT_STRATEGY);
  195. if (rc != 0) {
  196. RM_DEBUG("deflateInit2 init fail, %d\n", rc);
  197. DUMP_RAMDISK_STRUCT(target_ramdisk);
  198. assert(0);
  199. }
  200. def_target.avail_in = (uInt)total_cpio_merged_size; // size of input
  201. def_target.next_in = (Bytef *)ramdisk1->decomp_buf; // input char array
  202. def_target.avail_out = (uInt)target_ramdisk->comp_max_size; // size of output
  203. def_target.next_out = (Bytef *)target_ramdisk->comp_buf; // output char array
  204. PROFILING_START("ramdisk merge compression");
  205. rc = deflate(&def_target, Z_FINISH);
  206. PROFILING_END();
  207. if ((rc != Z_OK) && (rc != Z_STREAM_END)) {
  208. RM_DEBUG("Fail to compress merged ramdisk! result:%d\n", rc);
  209. DUMP_RAMDISK_STRUCT(target_ramdisk);
  210. assert(0);
  211. }
  212. rc = deflateEnd(&def_target);
  213. if (rc != Z_OK) {
  214. RM_DEBUG("Fail to complete merged ramdisk compression!\n");
  215. DUMP_RAMDISK_STRUCT(target_ramdisk);
  216. assert(0);
  217. }
  218. target_ramdisk->comp_size = def_target.total_out;
  219. RM_DEBUG("def_target.total_out:%lu\n", def_target.total_out);
  220. return 0;
  221. }
  222. /*
  223. * Validate cpio format
  224. * 1. So far we only support new ASCII format.
  225. * 2. Verify cpio_archive address is four bytes alignment.
  226. */
  227. static int validate_cpio_archive(const uint8_t *cpio_archive)
  228. {
  229. const cpio_newc_header_t *first_entry = (const cpio_newc_header_t *) cpio_archive;
  230. uintptr_t cpio_address = (uintptr_t) cpio_archive;
  231. if (memcmp(first_entry->c_magic, CPIO_NEW_C_MAGIC, CPIO_MAGIC_LEN) != 0)
  232. return -1;
  233. if (!IS_ALIGNED(cpio_address, CPIO_ALIGNMENT))
  234. return -2;
  235. return 0;
  236. }
  237. /* Find last entry from tail */
  238. static int find_cpio_last_entry(const uint8_t *cpio_archive, const uint32_t cpio_archive_size, uint8_t **last_entry)
  239. {
  240. const uint8_t *tail = (cpio_archive + (cpio_archive_size - 1));
  241. uint32_t search_size = 0;
  242. /* Padding bytes is '\0' */
  243. while (*tail == 0x0) {
  244. tail--;
  245. search_size++;
  246. }
  247. if (search_size > SEARCH_FROM_TAIL_SIZE)
  248. return -1;
  249. /*
  250. * Minus one is for NULL terminated string
  251. * Minus another one to point to head.
  252. */
  253. tail -= (CPIO_LAST_ENTRY_NAME_LEN - 2);
  254. /*
  255. * The string before padding bytes should be the last entry.
  256. * If not, this is invalid.
  257. */
  258. if (memcmp(tail, (void *) CPIO_LAST_ENTRY_NAME, CPIO_LAST_ENTRY_NAME_LEN) != 0)
  259. return -2;
  260. /* cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data */
  261. tail -= sizeof(cpio_newc_header_t);
  262. *last_entry = (uint8_t *)tail;
  263. return 0;
  264. }
  265. void dump_ramdisk_buf(ramdisk_buf_t *ramdisk, const char *func, int line)
  266. {
  267. RM_DEBUG("FUNC:%s, LINE:%d\n", func, line);
  268. RM_DEBUG("--------%s--------\n", __func__);
  269. RM_DEBUG("name:%s\n", ramdisk->name);
  270. RM_DEBUG("decomp_buf:0x%x\n", (uint32_t)ramdisk->decomp_buf);
  271. RM_DEBUG("decomp_size:%u\n", (uint32_t)ramdisk->decomp_size);
  272. RM_DEBUG("decomp_max_size:%u\n", (uint32_t)ramdisk->decomp_max_size);
  273. RM_DEBUG("comp_buf:0x%x\n", (uint32_t)ramdisk->comp_buf);
  274. RM_DEBUG("comp_size:%u\n", (uint32_t)ramdisk->comp_size);
  275. RM_DEBUG("comp_max_size:%u\n", (uint32_t)ramdisk->comp_max_size);
  276. }