/// \file sbe_default_tool.c /// \brief P9-XIP image setter tool for attributes in fixed section /// #include #include #include #include #include #include #include #include #include #include #include #define __PPE__ #include "fapi2.H" #include "proc_sbe_fixed.H" #include "p9_xip_image.h" const char* g_usage = "Usage: sbe_default_tool \n" "The 'image' is the binary image with fixed section.\n" "\n" "The 'attribute' is the attribute to be set.\n" "\n" "The 'value' is the value of the attribute to be set.\n" "\n" "The 'target type' is the type of the target. The following targets are defined:\n" "TARGET_TYPE_SYSTEM: system target\n" "TARGET_TYPE_PROC_CHIP: chip target\n" "TARGET_TYPE_PERV: pervasive target\n" "TARGET_TYPE_CORE: core target\n" "TARGET_TYPE_EQ: eq target\n" "TARGET_TYPE_EX: ex target\n" "\n" "The 'index' is the index of the value. Checking is performed.\n" "example:\n" "./sbe_default_tool ./sbe_main.bin ATTR_PLL_RING 0x33CAFE34 TARGET_TYPE_PERV 0\n" "./sbe_default_tool ./sbe_main.bin ATTR_SCRATCH_UINT8_1 12 TARGET_TYPE_PROC_CHIP 0\n" ; void assertTarget(const char* str, unsigned int index) { if(strcmp(str, "TARGET_TYPE_SYSTEM") == 0 || (strcmp(str, "TARGET_TYPE_PROC_CHIP") == 0)) { if (index > 0) { fprintf(stderr, "sbe_default_tool: index (%d) is larger than 0\n", index); exit(1); } } else if(strcmp(str, "TARGET_TYPE_EX") == 0) { if (index > EX_TARGET_COUNT) { fprintf(stderr, "sbe_default_tool: index (%d) is larger than EX_TARGET_COUNT (%d)\n", index, EX_TARGET_COUNT); exit(1); } } else if(strcmp(str, "TARGET_TYPE_EQ") == 0) { if (index > EQ_TARGET_COUNT) { fprintf(stderr, "sbe_default_tool: index (%d) is larger than EQ_TARGET_COUNT (%d)\n", index, EQ_TARGET_COUNT); exit(1); } } else if(strcmp(str, "TARGET_TYPE_CORE") == 0) { if (index > CORE_TARGET_COUNT) { fprintf(stderr, "sbe_default_tool: index (%d) is larger than CORE_TARGET_COUNT (%d)\n", index, CORE_TARGET_COUNT); exit(1); } } else if(strcmp(str, "TARGET_TYPE_PERV") == 0) { if (index > PERV_TARGET_COUNT) { fprintf(stderr, "sbe_default_tool: index (%d) is larger than PERV_TARGET_COUNT (%d)\n", index, PERV_TARGET_COUNT); exit(1); } } else { if (index >= PERV_TARGET_COUNT) { fprintf(stderr, "sbe_default_tool: target not supported:"); fprintf(stderr, " %s\n", str); exit(1); } } return; } void setAttribute(void* image, const char* attribute, unsigned int index, uint64_t val) { P9XipItem item; void *thePointer; int rc; rc = p9_xip_find(image, attribute, &item); if (rc) { fprintf(stderr, "sbe_default_tool: attribute not existing:"); fprintf(stderr, " %s", attribute); exit(1); } // debug purpose //printf("offset in string section: 0x%x \n", be32toh(item.iv_toc->iv_id)); //printf("address: 0x%x index: %2d 0x%x\n", item.iv_address, index, index); p9_xip_image2host(image, item.iv_address, &thePointer); // debug purpose //printf("pointer1: 0x%x val: 0x%llx \n", thePointer, val); if(item.iv_toc->iv_type == P9_XIP_UINT8) { *((uint8_t*)thePointer + (index * sizeof(uint8_t))) = (uint8_t)val; } else if(item.iv_toc->iv_type == P9_XIP_INT8) { *((int8_t*)thePointer + (index * sizeof(int8_t))) = (int8_t)val; } else if(item.iv_toc->iv_type == P9_XIP_UINT16) { *((uint16_t*)thePointer + (index * sizeof(uint16_t))) = xipRevLe16((uint16_t)val); } else if(item.iv_toc->iv_type == P9_XIP_INT16) { *((int16_t*)thePointer + (index * sizeof(int16_t))) = xipRevLe16((int16_t)val); } else if(item.iv_toc->iv_type == P9_XIP_UINT32) { *((uint32_t*)thePointer + (index * sizeof(uint32_t))) = xipRevLe32((uint32_t)val); } else if(item.iv_toc->iv_type == P9_XIP_INT32) { *((int32_t*)thePointer + (index * sizeof(int32_t))) = xipRevLe32((int32_t)val); } else if(item.iv_toc->iv_type == P9_XIP_UINT64) { *((uint64_t*)thePointer + (index * sizeof(uint64_t))) = xipRevLe64((uint64_t)val); } else if(item.iv_toc->iv_type == P9_XIP_INT64) { *((int64_t*)thePointer + (index * sizeof(int64_t))) = xipRevLe64((int64_t)val); } else { fprintf(stderr, "sbe_default_tool: type not available"); exit(1); } P9_XIP_SECTION_NAMES(section_name); P9_XIP_TYPE_STRINGS(type_name); // debug purpose //printf("pointer2: 0x%x \n", thePointer + index); //printf("elements: %d \n", item.iv_elements); //printf("section id: %s \n", section_name[item.iv_toc->iv_section]); //printf("location in section: 0x%x \n", be32toh(item.iv_toc->iv_data)); //printf("type name: %s \n", type_name[item.iv_toc->iv_type]); return; } uint64_t getAttribute(void* image, const char* attribute, unsigned int index) { uint64_t val = 0; P9XipItem item; void *thePointer; int rc; rc = p9_xip_find(image, attribute, &item); if (rc) { fprintf(stderr, "sbe_default_tool: attribute not existing:"); fprintf(stderr, " %s", attribute); exit(1); } p9_xip_image2host(image, item.iv_address, &thePointer); if(item.iv_toc->iv_type == P9_XIP_UINT8) { val = *((uint8_t*)thePointer + (index * sizeof(uint8_t))); } else if(item.iv_toc->iv_type == P9_XIP_INT8) { val = *((int8_t*)thePointer + (index * sizeof(int8_t))); val &= 0xFF; } else if(item.iv_toc->iv_type == P9_XIP_UINT16) { val = xipRevLe16(*((uint16_t*)thePointer + (index * sizeof(uint16_t)))); } else if(item.iv_toc->iv_type == P9_XIP_INT16) { val = xipRevLe16(*((int16_t*)thePointer + (index * sizeof(int16_t)))); val &= 0xFFFF; } else if(item.iv_toc->iv_type == P9_XIP_UINT32) { val = xipRevLe32(*((uint32_t*)thePointer + (index * sizeof(uint32_t)))); } else if(item.iv_toc->iv_type == P9_XIP_INT32) { val = xipRevLe32(*((int32_t*)thePointer + (index * sizeof(int32_t)))); val &= 0xFFFFFFFF; } else if(item.iv_toc->iv_type == P9_XIP_UINT64) { val = xipRevLe64(*((uint64_t*)thePointer + (index * sizeof(uint64_t)))); } else if(item.iv_toc->iv_type == P9_XIP_INT64) { val = xipRevLe64(*((int64_t*)thePointer + (index * sizeof(int64_t)))); } else { fprintf(stderr, "sbe_default_tool: type not available"); exit(1); } return val; } int main(int argc, const char** argv) { int fileFd, rc; void* image; struct stat buf; if(argc != 6) { fprintf(stderr, "sbe_default_tool: argument missing\n"); fprintf(stderr, g_usage); exit(1); } printf("sbe_default_tool %s %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4], argv[5]); fileFd = open(argv[1], O_RDWR); if (fileFd < 0) { fprintf(stderr, "sbe_default_tool: open() of the file to be appended failed"); exit(1); } rc = fstat(fileFd, &buf); if (rc) { fprintf(stderr, "sbe_default_tool: fstat() of the file to be appended failed"); exit(1); } image = mmap(0, buf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fileFd, 0); if (image == MAP_FAILED) { fprintf(stderr, "sbe_default_tool: mmap() of the file to be appended failed"); exit(1); } uint64_t val=strtoull(argv[3], 0, 0); unsigned int index = strtoul(argv[5], 0, 10); assertTarget(argv[4], index); setAttribute(image, argv[2], index, val); uint64_t check = getAttribute(image, argv[2], index); if((check & val) != check) { fprintf(stderr, "sbe_default_tool: set and get values not equal -> "); fprintf(stderr, "%lx != %lx\n", check, val); exit(1); } rc = close(fileFd); if (rc) { fprintf(stderr, "sbe_default_tool: close() of modified image failed"); exit(1); } return 0; }