From 236200c028dd48bbffbde1278d8b1c6de39fbdb1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 24 Sep 2021 09:43:15 -0700 Subject: [PATCH] altos: Fix config upgrade from 1.24 to 1.25 Alignment of pyro field changed due to increased size of fields which shifted the whole struct by two bytes, leading to mis-updating the struct. Fix that by creating a mirror of the ao_config type for version 1.24 and then using that to fetch the old data. Signed-off-by: Keith Packard --- src/kernel/ao_config.h | 64 ++++++++++++++++++++++++++++++++++++++++++ src/kernel/ao_pyro.c | 28 ++++++++++-------- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/kernel/ao_config.h b/src/kernel/ao_config.h index c668b71a..1d5a0fb0 100644 --- a/src/kernel/ao_config.h +++ b/src/kernel/ao_config.h @@ -128,6 +128,70 @@ struct ao_config { #endif }; +struct ao_config_1_24 { + uint8_t major; + uint8_t minor; + uint16_t main_deploy; + int16_t accel_plus_g; /* changed for minor version 2 */ + uint8_t _legacy_radio_channel; + char callsign[AO_MAX_CALLSIGN + 1]; + uint8_t apogee_delay; /* minor version 1 */ + int16_t accel_minus_g; /* minor version 2 */ + uint32_t radio_cal; /* minor version 3 */ + uint32_t flight_log_max; /* minor version 4 */ + uint8_t ignite_mode; /* minor version 5 */ + uint8_t pad_orientation; /* minor version 6 */ + uint32_t radio_setting; /* minor version 7 */ + uint8_t radio_enable; /* minor version 8 */ + uint8_t aes_key[AO_AES_LEN]; /* minor version 9 */ + uint32_t frequency; /* minor version 10 */ + uint16_t apogee_lockout; /* minor version 11 */ +#if AO_PYRO_NUM + struct ao_pyro_1_24 pyro[AO_PYRO_NUM]; /* minor version 12 */ +#endif + uint16_t aprs_interval; /* minor version 13 */ +#if HAS_RADIO_POWER + uint8_t radio_power; /* minor version 14 */ +#endif +#if HAS_RADIO_AMP + uint8_t radio_amp; /* minor version 14 */ +#endif +#if HAS_IMU + int16_t accel_zero_along; /* minor version 15 */ + int16_t accel_zero_across; /* minor version 15 */ + int16_t accel_zero_through; /* minor version 15 */ +#endif +#if HAS_BEEP + uint8_t mid_beep; /* minor version 16 */ +#endif +#if HAS_TRACKER + uint16_t tracker_motion; /* minor version 17 */ + uint8_t tracker_interval; /* minor version 17 */ +#endif +#if AO_PYRO_NUM + uint16_t pyro_time; /* minor version 18 */ +#endif +#if HAS_APRS + uint8_t aprs_ssid; /* minor version 19 */ +#endif +#if HAS_RADIO_RATE + uint8_t radio_rate; /* minor version 20 */ +#endif +#if HAS_RADIO_FORWARD + uint32_t send_frequency; /* minor version 21 */ +#endif +#if HAS_APRS + uint8_t aprs_format; /* minor version 22 */ +#endif +#if HAS_FIXED_PAD_BOX + uint8_t pad_box; /* minor version 22 */ + uint8_t pad_idle; /* minor version 23 */ +#endif +#if HAS_APRS + uint8_t aprs_offset; /* minor version 24 */ +#endif +}; + #define AO_APRS_FORMAT_COMPRESSED 0 #define AO_APRS_FORMAT_UNCOMPRESSED 1 #define AO_CONFIG_DEFAULT_APRS_FORMAT AO_APRS_FORMAT_COMPRESSED diff --git a/src/kernel/ao_pyro.c b/src/kernel/ao_pyro.c index 07a8278d..61604100 100644 --- a/src/kernel/ao_pyro.c +++ b/src/kernel/ao_pyro.c @@ -640,18 +640,22 @@ ao_pyro_update_version(void) /* First, move all of the config bits that follow the pyro data */ - char *pyro_base = (void *) &ao_config.pyro; - char *after_pyro_new = pyro_base + AO_PYRO_NUM * sizeof (struct ao_pyro); - char *after_pyro_1_24 = pyro_base + AO_PYRO_NUM * sizeof (struct ao_pyro_1_24); - char *config_end = (void *) (&ao_config + 1); - size_t to_move = config_end - after_pyro_new; + struct ao_config_1_24 *ao_config_1_24 = (void *) &ao_config; + struct ao_pyro *pyro_1_25 = &ao_config.pyro[0]; + struct ao_pyro_1_24 *pyro_1_24 = &(ao_config_1_24->pyro)[0]; - memmove(after_pyro_new, after_pyro_1_24, to_move); - /* Now, adjust all of the pyro entries */ + char *pyro_base_1_25 = (void *) pyro_1_25; + char *pyro_base_1_24 = (void *) pyro_1_24; + char *after_pyro_1_25 = pyro_base_1_25 + AO_PYRO_NUM * sizeof (struct ao_pyro); + char *after_pyro_1_24 = pyro_base_1_24 + AO_PYRO_NUM * sizeof (struct ao_pyro_1_24); + + char *config_end_1_25 = (void *) (&ao_config + 1); + size_t to_move = config_end_1_25 - after_pyro_1_25; - struct ao_pyro *pyro_new = ao_config.pyro; - struct ao_pyro_1_24 *pyro_old = (void *) ao_config.pyro; + memmove(after_pyro_1_25, after_pyro_1_24, to_move); + + /* Now, adjust all of the pyro entries */ int p = AO_PYRO_NUM; @@ -664,15 +668,15 @@ ao_pyro_update_version(void) struct ao_pyro tmp; memset(&tmp, '\0', sizeof(tmp)); - tmp.flags = pyro_old[p].flags; + tmp.flags = pyro_1_24[p].flags; for (v = 0; v < NUM_PYRO_VALUES; v++) { - value = ao_pyro_get_1_24(&pyro_old[v], ao_pyro_values[v].flag); + value = ao_pyro_get_1_24(&pyro_1_24[p], ao_pyro_values[v].flag); ao_pyro_put(&tmp, ao_pyro_values[v].offset, ao_pyro_size(ao_pyro_values[v].flag), value); } - memcpy(&pyro_new[p], &tmp, sizeof(tmp)); + memcpy(&pyro_1_25[p], &tmp, sizeof(tmp)); } } } -- 2.30.2