altos: Fix config upgrade from 1.24 to 1.25
authorKeith Packard <keithp@keithp.com>
Fri, 24 Sep 2021 16:43:15 +0000 (09:43 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 24 Sep 2021 23:30:51 +0000 (16:30 -0700)
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 <keithp@keithp.com>
src/kernel/ao_config.h
src/kernel/ao_pyro.c

index c668b71a1979c418c5f39b145b8e72cb9a65b95e..1d5a0fb022deab4964b97f857769adf66b867ece 100644 (file)
@@ -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
index 07a8278d560a85f4d913ccf7a735645131d31cc6..6160410000d9d0af2a5733a811e67d38c1c73ab4 100644 (file)
@@ -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));
                }
        }
 }