Recovering members of a struct

I’ve been assigned the task of trying to recover the data from a struct after the size of it has changed.

i.e. Our struct has 146Bytes in it and gets saved to the user block, after a firmware upgrade the size of the struct may have gotten larger and we need to read the struct from the user block and try to recover the values to populate the new struct.

Is this possible? I’m thinking yes as long as the new members are only added to the end of the new struct so that all the old data will still be in the same positions.

Would the best method then be to set the new struct to factory defaults and then use memcpy to populate the new struct? In this way all new members would be initialized as well.

Ron

What you suggested should work.

However, if the existing struct does not have a version number field, you should take this opportunity to add one. Place it at an offset that you know will not change in the future. This will allow you in the future to do arbitrary upgrades from any old version to the current version, should the structure change again. Trying to determine the version of an older structure that doesn’t contain the version number can get interesting…

Yes, I actually use the version of code as the first and last member of the struct so that I can verify validity of the members (in case of power failure during a UB write and such)

Ron

I have a problem here:

printf("UB address offset %d
", &UBconfigData + (UBCodesize-sizeof(configData.code2))); 

&UBconfigData =0xDF02
(UBCodesize-sizeof(configData.code2)) =0x8F

0xDF02 + 0x8f = DF91

and I can verify at this address is the data I need but

&UBconfigData + (UBCodesize-sizeof(configData.code2))

returns 0x67DD

btw if you didn’t figure it out UBconfigData is a structure.

is there something about address arithmetic I’m missing?

Ron

I feel I must give more information,

When our firmware gets updated, we take our configuration struct get the size of it, write that and then save the struct to the userblock. Update the firmware then read in the size and read in the userblock and restore the configuration data.

Now our struct has increased in size (146 Bytes to 245 Bytes) and I still want to retrieve this data but since our struct has changed in size the type def in no longer valid for the UB data. We write a string to members name .code1 and .code2 to the beginning and end respectively.

After an upgrade and reading in the UB I need to verify that it is valid so I get the size of the data I read in and minus the size of .code 2. This will give me the starting offset for .code 2 so I can match it to .code 1.

My problem lies in I don’t know how to access the info located at the offset in the struct I read in from the userblock.

Hope that helps

Ron

Try ((char *) &UBConfigData) + (UBCodesize-sizeof(configData.code2)));

If you add something to a structure pointer, it treats it like you’re accessing an array of structures of that type - the offset gets multiplied by sizeof(UBConfigData)…

Thanks so much!!!

It works like a charm.

Ron