| /* |
| * (C) Copyright 2018 |
| * Marvell Semiconductor <www.ASR.com> |
| * |
| * SPDX-License-Identifier: GPL-2.0+ |
| */ |
| |
| #include "mrd.h" |
| |
| /*********************************************************************** |
| * Function: MRDInternalFileHeaderOffsetGet * |
| ************************************************************************ |
| * Description: Get file offset of MRD_filename in MRD * |
| * * |
| * Parameters: ctx - MRD context * |
| * MRD_filename - internal MRD filename * |
| * MRD_file_buffer - MRD buffer * |
| * mrd_file_type - MRD file type * |
| * file_size - the raw file size inside the MRD * |
| * * |
| * Return value: offset - the file MRD header offset from MRD base, * |
| * if the file was not found in MRD, offset=0 * |
| * * |
| * Notes: * |
| ***********************************************************************/ |
| u32 MRDFileHeaderOffsetGet( unsigned char *MRD_file_buffer, |
| char *MRD_filename, |
| u32 mrd_file_type, |
| u32 *file_size) |
| { |
| MRD_entry_header_t *pMRD_file_hdr; |
| unsigned char *ptr; |
| u8 valid_file; |
| u8 file_found; |
| u32 offset; |
| u32 pad_size; |
| u32 compare_result; |
| |
| /* skip MRD image header */ |
| ptr = (unsigned char *)MRD_file_buffer + sizeof(MRD_header_t); |
| |
| /* scan until reaching the end of MRD */ |
| offset = 0; |
| valid_file = 1; |
| file_found = 0; |
| |
| do { |
| pMRD_file_hdr = (MRD_entry_header_t *)ptr; |
| #if 0 |
| if (MRD_NO_ERROR != MRDIsValidFileHeader(pMRD_file_hdr)) { |
| DPRINTF("not MRD valid file header!\n"); |
| valid_file = FALSE; |
| break; |
| } |
| #endif |
| if (pMRD_file_hdr->entry_type == END_BUF_STMP) |
| { |
| valid_file = 0; |
| } |
| /* Calculate pad size */ |
| //pad_size = sizeof(u32) - (pMRD_file_hdr->entry_size.file_size % sizeof(u32)); |
| // pad_size = (sizeof(u32) - (pMRD_file_hdr->entry_size & 0x03)) & 0x3; |
| |
| // 8 MSB field is padding size |
| pad_size = (pMRD_file_hdr->entry_size >> 24) & 0x3; |
| |
| /* Support searching for tail header */ |
| if (MRD_filename != NULL) |
| { |
| if(mrd_file_type == MRD_IMEI_TYPE) |
| { |
| // to keep compatible with single IMEI code |
| // only strict compare the IMEI2 name |
| // so for IMEI1, we only need to ensure current file is not IMEI2 |
| if(strcmp(MRD_filename, IMEI2_NUM_FILE_NAME) == 0) |
| compare_result = strcmp(MRD_filename, pMRD_file_hdr->fileName); |
| else |
| compare_result = !strcmp(IMEI2_NUM_FILE_NAME, pMRD_file_hdr->fileName); |
| } |
| else if(mrd_file_type == MRD_MEP_TYPE) |
| { |
| // to keep compatible with single MEP code |
| // only strict compare the MEP2 name |
| // so for MEP1, we only need to ensure current file is not MEP2 |
| if(strcmp(MRD_filename, MEP2_FILE_NAME) == 0) |
| compare_result = strcmp(MRD_filename, pMRD_file_hdr->fileName); |
| else |
| compare_result = !strcmp(MEP2_FILE_NAME, pMRD_file_hdr->fileName); |
| } |
| else |
| { |
| compare_result = strcmp(MRD_filename, pMRD_file_hdr->fileName); |
| } |
| } |
| else |
| { |
| compare_result = 0; |
| } |
| |
| if ( (mrd_file_type == pMRD_file_hdr->entry_type) && |
| (0 == compare_result) ) |
| { |
| offset = (u32)((u32)ptr - (u32)MRD_file_buffer); |
| *file_size = (pMRD_file_hdr->entry_size & 0xFFFFFF); |
| *file_size -= pad_size; // file_size is raw file size |
| file_found = 1; |
| } |
| |
| if (valid_file && file_found) { |
| printf("Found MRD file entry:\n"); |
| printf("====================\n"); |
| printf("entry_size=0x%x\n", pMRD_file_hdr->entry_size); |
| //DPRINTF("entry_size.pad_size=0x%x\n", pMRD_file_hdr->entry_size.pad_size); |
| printf("entry_type=0x%x\n", pMRD_file_hdr->entry_type); |
| printf("fileName=%s\n", pMRD_file_hdr->fileName); |
| printf("timestamp=0x%x\n", pMRD_file_hdr->timestamp); |
| printf("header_checksum=0x%x\n", pMRD_file_hdr->header_checksum); |
| printf("version=0x%x\n\n", pMRD_file_hdr->version); |
| } |
| |
| ptr += sizeof(MRD_entry_header_t); |
| ptr += (pMRD_file_hdr->entry_size & 0xFFFFFF); //.file_size; |
| //ptr += pad_size; |
| } while ( (valid_file != 0) && |
| (file_found == 0) && |
| ((u32)ptr < ((u32)MRD_file_buffer + MRD_MAX_FILE_LEN)) ); |
| |
| return offset; |
| } |
| |
| /*********************************************************************** |
| * Function: MRDFileOffsetGet * |
| ************************************************************************ |
| * Description: Get file offset of MRD_filename in MRD * |
| * * |
| * Parameters: ctx - MRD context * |
| * MRD_filename - internal MRD filename * |
| * MRD_file_buffer - MRD buffer * |
| * mrd_file_type - MRD file type * |
| * file_size - the raw file size inside the MRD * |
| * * |
| * Return value: offset - the file offset from MRD base, * |
| * if the file was not found in MRD, offset=0 * |
| * * |
| * Notes: * |
| ***********************************************************************/ |
| u32 MRDFileOffsetGet(unsigned char *MRD_file_buffer, |
| char *MRD_filename, |
| u32 mrd_file_type, |
| u32 *file_size) |
| { |
| //MRD_entry_header_t *pMRD_file_hdr; |
| u32 offset; |
| |
| offset = MRDFileHeaderOffsetGet( MRD_file_buffer, |
| MRD_filename, |
| mrd_file_type, |
| file_size); |
| /* if no error, continue */ |
| if (offset != 0) { |
| //pMRD_file_hdr = (MRD_entry_header_t *)((char *)MRD_file_buffer + offset); |
| |
| /* Calculate pad size */ |
| //pad_size = ((sizeof(u32) - (pMRD_file_hdr->entry_size.file_size & 0x3)) & 0x3); |
| offset += sizeof(MRD_entry_header_t); |
| //offset += pad_size; |
| } |
| |
| return offset; |
| } |