mrdump_datafs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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) 2016. 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 <string.h>
  34. #include <malloc.h>
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <platform/mt_typedefs.h>
  38. #include <platform/mtk_wdt.h>
  39. #include <lib/zlib.h>
  40. #include "aee.h"
  41. #include "mrdump_private.h"
  42. #define BLKSIZE 4096
  43. #define MAX_CONTINUE 16 // only 1MB memory for lk
  44. #define EXSPACE (BLKSIZE*MAX_CONTINUE) // Expect continue space
  45. uint64_t lba_marker_time;
  46. struct __attribute__((__packed__)) marked_block_data {
  47. uint32_t lba;
  48. uint64_t zero_padding[510];
  49. uint64_t timestamp;
  50. uint32_t crc;
  51. } bdata;
  52. struct mrdump_lba_handle {
  53. struct mrdump_dev *dumpdev;
  54. uint64_t allocsize;
  55. uint64_t filesize;
  56. unsigned int wlba;
  57. unsigned int rlba;
  58. int bidx;
  59. int midx;
  60. unsigned int blknum;
  61. unsigned int block_lba[1024];
  62. uint8_t data[EXSPACE];
  63. };
  64. static uint64_t ext4_lba_to_block_offset(uint32_t lba)
  65. {
  66. return (uint64_t)lba * (uint64_t)BLKSIZE;
  67. }
  68. static bool check_block_valid(const struct mrdump_lba_handle *handle, uint32_t lba)
  69. {
  70. mtk_wdt_restart();
  71. if (!handle->dumpdev->read(handle->dumpdev, ext4_lba_to_block_offset(lba), (uint8_t *)&bdata, BLKSIZE)) {
  72. voprintf_error(" Read BlockData failed\n");
  73. return false;
  74. }
  75. if (bdata.lba != lba) {
  76. voprintf_error(" Read BlockData LBA failed (c:%08x, v:%08x)\n", bdata.lba, lba);
  77. return false;
  78. }
  79. if (bdata.timestamp != lba_marker_time) {
  80. voprintf_error(" Read BlockData timestamp failed (c:%016x, v:%016x)\n", bdata.timestamp, lba_marker_time);
  81. return false;
  82. }
  83. unsigned int crcval = crc32(0, Z_NULL, 0);
  84. crcval = crc32(crcval, (void *)&bdata, (BLKSIZE - 4));
  85. if (bdata.crc != crcval) {
  86. voprintf_error("%s: Get BlockData crc32 error (c:%08x, v:%08x)\n", __func__, crcval, bdata.crc);
  87. return false;
  88. }
  89. return true;
  90. }
  91. static int Get_Next_bidx(struct mrdump_lba_handle *handle, unsigned int moves)
  92. {
  93. unsigned int mycrc;
  94. if (handle->bidx == 1022) {
  95. handle->rlba = handle->block_lba[handle->bidx];
  96. if (!handle->dumpdev->read(handle->dumpdev, ext4_lba_to_block_offset(handle->rlba), (uint8_t *)handle->block_lba, BLKSIZE)) {
  97. voprintf_error(" SDCard: Reading BlockLBA failed.\n");
  98. return -1;
  99. }
  100. // check crc32
  101. mycrc = crc32(0, Z_NULL, 0);
  102. mycrc = crc32(mycrc, (void *)handle->block_lba, (BLKSIZE-4));
  103. if (mycrc != handle->block_lba[1023]) {
  104. voprintf_error(" Get next index crc32 error!\n");
  105. return -1;
  106. }
  107. handle->bidx = 0;
  108. } else {
  109. handle->bidx+=moves;
  110. }
  111. return handle->bidx;
  112. }
  113. unsigned int Num_to_Join(const struct mrdump_lba_handle *handle, unsigned int idx)
  114. {
  115. unsigned int i, j;
  116. uint32_t lba;
  117. for (i=0, j=0; i<MAX_CONTINUE; i++) {
  118. lba = handle->block_lba[idx+i];
  119. if ((lba - handle->block_lba[idx]) == i) {
  120. mtk_wdt_restart();
  121. if (!check_block_valid(handle, lba)) {
  122. voprintf_error(" BlockData Verification failed\n");
  123. return 0;
  124. }
  125. j++;
  126. continue;
  127. }
  128. break;
  129. }
  130. return j;
  131. }
  132. // Store data and write when buffer(handle's data) full
  133. // return left length to avoid recursive call. --> turn to for loop
  134. static int Do_Store_or_Write(struct mrdump_lba_handle *handle, uint8_t *buf, uint32_t Length)
  135. {
  136. int total;
  137. unsigned int leftspace, mylen, reval;
  138. total = BLKSIZE * handle->blknum;
  139. leftspace = total - handle->midx;
  140. // Check Length
  141. if (Length > leftspace) {
  142. mylen = leftspace;
  143. reval = Length - leftspace;
  144. } else {
  145. mylen = Length;
  146. reval = 0;
  147. }
  148. // Store
  149. memcpy(handle->data + handle->midx, buf, mylen);
  150. handle->midx += mylen;
  151. // Write
  152. if (handle->midx == total) {
  153. if (!handle->dumpdev->write(handle->dumpdev, ext4_lba_to_block_offset(handle->wlba), handle->data, total)) {
  154. voprintf_error(" SDCard: Write dump data failed.\n");
  155. return -1;
  156. }
  157. handle->bidx = Get_Next_bidx(handle, handle->blknum);
  158. if (handle->bidx < 0) {
  159. voprintf_error(" SDCard: Reading bidx failed.\n");
  160. return -1;
  161. }
  162. if (handle->bidx == 1022) {
  163. handle->bidx = Get_Next_bidx(handle, handle->blknum);
  164. if (handle->bidx < 0) {
  165. voprintf_error(" SDCard: Reading 1022 bidx failed.\n");
  166. return -1;
  167. }
  168. }
  169. handle->blknum = Num_to_Join(handle, handle->bidx);
  170. if (handle->blknum == 0)
  171. return -1;
  172. handle->wlba = handle->block_lba[handle->bidx];
  173. handle->midx = 0;
  174. }
  175. return reval;
  176. }
  177. static int lba_write_cb(void *opaque_handle, void *buf, int size)
  178. {
  179. unsigned int len;
  180. int ret;
  181. uint8_t *Ptr;
  182. struct mrdump_lba_handle *handle = opaque_handle;
  183. if ((handle->filesize + size) > handle->allocsize) {
  184. voprintf_error(" dump size > allocated size. file-size %lld allocate-size %lld\n", handle->filesize + size, handle->allocsize);
  185. return -1;
  186. }
  187. handle->filesize += size;
  188. // End of File, write the left Data in handle data buffer...
  189. if ((buf == NULL) && (size == 0)) {
  190. if (!handle->dumpdev->write(handle->dumpdev, ext4_lba_to_block_offset(handle->wlba), handle->data, handle->midx)) {
  191. voprintf_error(" SDCard: Write dump data failed.\n");
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. ASSERT(buf != NULL);
  197. // process of Store and write
  198. len = size;
  199. ret = len;
  200. Ptr = (uint8_t *)buf;
  201. while (1) {
  202. ret = Do_Store_or_Write(handle, Ptr, len);
  203. if (ret < 0) {
  204. voprintf_error(" SDCard: Store and Write failed.\n");
  205. return -1;
  206. } else if (ret == 0) {
  207. break;
  208. } else {
  209. Ptr += (len - ret);
  210. len = ret;
  211. }
  212. }
  213. return size;
  214. }
  215. static inline uint16_t paf_version_extract(uint8_t pafinfo[MRDUMP_PAF_TOTAL_SIZE])
  216. {
  217. return pafinfo[0] + (pafinfo[1] << 8);
  218. }
  219. static void dump_paf_info(uint8_t InfoLBA[MRDUMP_PAF_TOTAL_SIZE], const char *msg, ...)
  220. {
  221. va_list ap;
  222. va_start(ap, msg);
  223. voprintf('E', msg, ap);
  224. va_end(ap);
  225. uint32_t paf_version = paf_version_extract(InfoLBA);
  226. uint32_t paf_info_lba = *(uint32_t *)(InfoLBA + MRDUMP_PAF_INFO_LBA);
  227. uint32_t paf_addr_lba = *(uint32_t *)(InfoLBA + MRDUMP_PAF_ADDR_LBA);
  228. uint64_t paf_alloc_size = *(uint64_t *)(InfoLBA + MRDUMP_PAF_ALLOCSIZE);
  229. uint64_t paf_coredump_size = *(uint64_t *)(InfoLBA + MRDUMP_PAF_COREDUMPSIZE);
  230. uint64_t paf_timestamp = *(uint64_t *)(InfoLBA + MRDUMP_PAF_TIMESTAMP);
  231. uint32_t paf_crc32 = *(uint32_t *)(InfoLBA + MRDUMP_PAF_CRC32);
  232. voprintf_info(" pafile ver %u info-lba %u addr-lba %u alloc-size %llu coredump-size %llu timestamp %llx crc %x\n",
  233. paf_version, paf_info_lba, paf_addr_lba, paf_alloc_size, paf_coredump_size, paf_timestamp, paf_crc32);
  234. }
  235. int mrdump_ext4_output(const struct mrdump_control_block *mrdump_cb, const struct kzip_addlist *memlist, const struct kzip_addlist *memlist_cmm, struct mrdump_dev *mrdump_dev)
  236. {
  237. uint8_t InfoLBA[MRDUMP_PAF_TOTAL_SIZE];
  238. unsigned int mycrc;
  239. if (mrdump_dev == NULL) {
  240. return -1;
  241. }
  242. voprintf_info("Output to DATA FS Partition %s\n", mrdump_dev->name);
  243. // pre-work for ext4 LBA
  244. bzero(InfoLBA, sizeof(InfoLBA));
  245. // Error 1. InfoLBA starting address not available
  246. if (mrdump_cb->output_fs_lbaooo == 0) {
  247. voprintf_error(" Pre-Allocate has no LBA markers(lbaooo=%u). RAM-Dump stop!\n", mrdump_cb->output_fs_lbaooo);
  248. return -1;
  249. }
  250. if (!mrdump_dev->read(mrdump_dev, ext4_lba_to_block_offset(mrdump_cb->output_fs_lbaooo), (uint8_t *)InfoLBA, sizeof(InfoLBA))) {
  251. voprintf_error(" SDCard: Reading InfoLBA failed.\n");
  252. return -1;
  253. }
  254. uint32_t paf_version = paf_version_extract(InfoLBA);
  255. uint32_t paf_info_lba = *(uint32_t *)(InfoLBA + MRDUMP_PAF_INFO_LBA);
  256. uint32_t paf_addr_lba = *(uint32_t *)(InfoLBA + MRDUMP_PAF_ADDR_LBA);
  257. uint64_t paf_alloc_size = *(uint64_t *)(InfoLBA + MRDUMP_PAF_ALLOCSIZE);
  258. uint64_t paf_coredump_size = *(uint64_t *)(InfoLBA + MRDUMP_PAF_COREDUMPSIZE);
  259. uint64_t paf_timestamp = *(uint64_t *)(InfoLBA + MRDUMP_PAF_TIMESTAMP);
  260. uint32_t paf_info_crc32 = *(uint32_t *)(InfoLBA + MRDUMP_PAF_CRC32);
  261. if (paf_version != MRDUMP_PAF_VERSION) {
  262. dump_paf_info(InfoLBA, " Unsupport PAF version %d (expected: %d)\n", paf_version, MRDUMP_PAF_VERSION);
  263. return -1;
  264. }
  265. // Error 3. InfoLBA[EXT4_1ST_LBA] should be mrdump_cb->output_fs_lbaooo
  266. if (mrdump_cb->output_fs_lbaooo != paf_info_lba) {
  267. dump_paf_info(InfoLBA, " LBA Starting Address Error(LBA0=%u)! Abort!\n", paf_info_lba);
  268. return -1;
  269. }
  270. // Error 4. EXT4_CORE_DUMP_SIZE is not zero --> want to hold 1st dump
  271. if (paf_coredump_size != 0) {
  272. dump_paf_info(InfoLBA, " CORE DUMP SIZE is not Zero! Abort!(coresize=%llu)\n", paf_coredump_size);
  273. return -1;
  274. }
  275. // Error 5. EXT4_USER_FILESIZE is zero
  276. if (paf_alloc_size == 0) {
  277. dump_paf_info(InfoLBA, " Allocate file with zero size. Abort!(filesize=%llu)\n", paf_alloc_size);
  278. return -1;
  279. }
  280. // get lba_marker_time as timestamp
  281. lba_marker_time = paf_timestamp;
  282. // Error 2. CRC not matched
  283. mycrc = crc32(0L, Z_NULL, 0);
  284. mycrc = crc32(mycrc, (const unsigned char*)InfoLBA, sizeof(InfoLBA) - 4);
  285. if (mycrc != paf_info_crc32) {
  286. dump_paf_info(InfoLBA, " InfoLBA CRC32 Error! Abort! (CRC1=0x%08x, CRC2=0x%08x)\n", mycrc, paf_info_crc32);
  287. return -1;
  288. }
  289. struct mrdump_lba_handle *handle = memalign(16, sizeof(struct mrdump_lba_handle));
  290. if (handle == NULL) {
  291. voprintf_error("No enough memory.\n");
  292. return -1;
  293. }
  294. memset(handle, 0, sizeof(struct mrdump_lba_handle));
  295. handle->dumpdev = mrdump_dev;
  296. handle->rlba = mrdump_cb->output_fs_lbaooo;
  297. handle->allocsize = paf_alloc_size;
  298. // Mark coredump as ULLONG_MAX to verify the unknown interrupt case during dumping data
  299. *(uint64_t *)(InfoLBA + MRDUMP_PAF_COREDUMPSIZE) = ULLONG_MAX;
  300. //calculate the CRC
  301. mycrc = crc32(0L, Z_NULL, 0);
  302. *(uint32_t *)(InfoLBA + MRDUMP_PAF_CRC32) = crc32(mycrc, (const unsigned char*)InfoLBA, MRDUMP_LBA_DATAONLY);
  303. //update the coredump sizeand CRC
  304. if (!handle->dumpdev->write(handle->dumpdev, ext4_lba_to_block_offset(paf_info_lba), (uint8_t *)InfoLBA, sizeof(InfoLBA))) {
  305. voprintf_error(" SDCard: Write InfoLBA error.\n");
  306. free(handle);
  307. return -1;
  308. }
  309. // Starting Dumping Data
  310. handle->rlba = paf_addr_lba;
  311. if (!handle->dumpdev->read(handle->dumpdev, ext4_lba_to_block_offset(handle->rlba), (uint8_t *)handle->block_lba, BLKSIZE)) {
  312. dump_paf_info(InfoLBA, " SDCard: Reading BlockLBA error.\n");
  313. free(handle);
  314. return -1;
  315. }
  316. handle->wlba = handle->block_lba[handle->midx];
  317. handle->blknum = Num_to_Join(handle, handle->bidx);
  318. if (handle->blknum == 0) {
  319. voprintf_error("No continuous space.\n");
  320. free(handle);
  321. return -1;
  322. }
  323. voprintf_info(" Pre-Allocate starts at LBA: %u\n", paf_info_lba);
  324. voprintf_info(" SYS_COREDUMP starts at LBA: %u\n", handle->wlba);
  325. mtk_wdt_restart();
  326. bool ok = true;
  327. mtk_wdt_restart();
  328. struct kzip_file *zf = kzip_open(handle, lba_write_cb);
  329. if (zf != NULL) {
  330. if (!kzip_add_file(zf, memlist, "SYS_COREDUMP")) {
  331. ok = false;
  332. }
  333. if (ok && !kzip_add_file(zf, memlist_cmm, "kernel.cmm")) {
  334. ok = false;
  335. }
  336. mtk_wdt_restart();
  337. kzip_close(zf);
  338. lba_write_cb(handle, NULL, 0); /* really write onto emmc of the last part */
  339. zf = NULL;
  340. } else {
  341. ok = false;
  342. }
  343. if (!ok) {
  344. free(handle);
  345. return -1;
  346. }
  347. voprintf_info(" SYS_COREDUMP ends at LBA: %u\n", handle->wlba);
  348. voprintf_info(" Zip COREDUMP size is: %llu\n", handle->filesize);
  349. // Record File Size...
  350. *(uint64_t *)(InfoLBA + MRDUMP_PAF_COREDUMPSIZE) = handle->filesize;
  351. mycrc = crc32(0L, Z_NULL, 0);
  352. *(uint32_t *)(InfoLBA + MRDUMP_PAF_CRC32) = crc32(mycrc, (const unsigned char*)InfoLBA, MRDUMP_LBA_DATAONLY);
  353. if (!handle->dumpdev->write(handle->dumpdev, ext4_lba_to_block_offset(paf_info_lba), (uint8_t *)InfoLBA, sizeof(InfoLBA))) {
  354. voprintf_error(" SDCard: Write InfoLBA error.\n");
  355. free(handle);
  356. return -1;
  357. }
  358. free(handle);
  359. mtk_wdt_restart();
  360. mrdump_status_ok("OUTPUT:%s\nMODE:%s\n", "DATA_FS", mrdump_mode2string(mrdump_cb->crash_record.reboot_mode));
  361. return 0;
  362. }