11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest testcase for parallel flash with AMD command set
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2019 Stephen Checkoway
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
71e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
81e8a1faeSThomas Huth  */
91e8a1faeSThomas Huth 
101e8a1faeSThomas Huth #include "qemu/osdep.h"
11907b5105SMarc-André Lureau #include "libqtest.h"
121e8a1faeSThomas Huth 
131e8a1faeSThomas Huth /*
141e8a1faeSThomas Huth  * To test the pflash_cfi02 device, we run QEMU with the musicpal machine with
151e8a1faeSThomas Huth  * a pflash drive. This enables us to test some flash configurations, but not
161e8a1faeSThomas Huth  * all. In particular, we're limited to a 16-bit wide flash device.
171e8a1faeSThomas Huth  */
181e8a1faeSThomas Huth 
191e8a1faeSThomas Huth #define MP_FLASH_SIZE_MAX (32 * 1024 * 1024)
201e8a1faeSThomas Huth #define BASE_ADDR (0x100000000ULL - MP_FLASH_SIZE_MAX)
211e8a1faeSThomas Huth 
221e8a1faeSThomas Huth #define UNIFORM_FLASH_SIZE (8 * 1024 * 1024)
231e8a1faeSThomas Huth #define UNIFORM_FLASH_SECTOR_SIZE (64 * 1024)
241e8a1faeSThomas Huth 
251e8a1faeSThomas Huth /* Use a newtype to keep flash addresses separate from byte addresses. */
261e8a1faeSThomas Huth typedef struct {
271e8a1faeSThomas Huth     uint64_t addr;
281e8a1faeSThomas Huth } faddr;
291e8a1faeSThomas Huth #define FLASH_ADDR(x) ((faddr) { .addr = (x) })
301e8a1faeSThomas Huth 
311e8a1faeSThomas Huth #define CFI_ADDR FLASH_ADDR(0x55)
321e8a1faeSThomas Huth #define UNLOCK0_ADDR FLASH_ADDR(0x555)
331e8a1faeSThomas Huth #define UNLOCK1_ADDR FLASH_ADDR(0x2AA)
341e8a1faeSThomas Huth 
351e8a1faeSThomas Huth #define CFI_CMD 0x98
361e8a1faeSThomas Huth #define UNLOCK0_CMD 0xAA
371e8a1faeSThomas Huth #define UNLOCK1_CMD 0x55
381e8a1faeSThomas Huth #define SECOND_UNLOCK_CMD 0x80
391e8a1faeSThomas Huth #define AUTOSELECT_CMD 0x90
401e8a1faeSThomas Huth #define RESET_CMD 0xF0
411e8a1faeSThomas Huth #define PROGRAM_CMD 0xA0
421e8a1faeSThomas Huth #define SECTOR_ERASE_CMD 0x30
431e8a1faeSThomas Huth #define CHIP_ERASE_CMD 0x10
441e8a1faeSThomas Huth #define UNLOCK_BYPASS_CMD 0x20
451e8a1faeSThomas Huth #define UNLOCK_BYPASS_RESET_CMD 0x00
461e8a1faeSThomas Huth #define ERASE_SUSPEND_CMD 0xB0
471e8a1faeSThomas Huth #define ERASE_RESUME_CMD SECTOR_ERASE_CMD
481e8a1faeSThomas Huth 
491e8a1faeSThomas Huth typedef struct {
501e8a1faeSThomas Huth     int bank_width;
511e8a1faeSThomas Huth 
521e8a1faeSThomas Huth     /* Nonuniform block size. */
531e8a1faeSThomas Huth     int nb_blocs[4];
541e8a1faeSThomas Huth     int sector_len[4];
551e8a1faeSThomas Huth 
561e8a1faeSThomas Huth     QTestState *qtest;
571e8a1faeSThomas Huth } FlashConfig;
581e8a1faeSThomas Huth 
598189b27dSBin Meng static char *image_path;
601e8a1faeSThomas Huth 
611e8a1faeSThomas Huth /*
621e8a1faeSThomas Huth  * The pflash implementation allows some parameters to be unspecified. We want
631e8a1faeSThomas Huth  * to test those configurations but we also need to know the real values in
641e8a1faeSThomas Huth  * our testing code. So after we launch qemu, we'll need a new FlashConfig
651e8a1faeSThomas Huth  * with the correct values filled in.
661e8a1faeSThomas Huth  */
expand_config_defaults(const FlashConfig * c)671e8a1faeSThomas Huth static FlashConfig expand_config_defaults(const FlashConfig *c)
681e8a1faeSThomas Huth {
691e8a1faeSThomas Huth     FlashConfig ret = *c;
701e8a1faeSThomas Huth 
711e8a1faeSThomas Huth     if (ret.bank_width == 0) {
721e8a1faeSThomas Huth         ret.bank_width = 2;
731e8a1faeSThomas Huth     }
741e8a1faeSThomas Huth     if (ret.nb_blocs[0] == 0 && ret.sector_len[0] == 0) {
751e8a1faeSThomas Huth         ret.sector_len[0] = UNIFORM_FLASH_SECTOR_SIZE;
761e8a1faeSThomas Huth         ret.nb_blocs[0] = UNIFORM_FLASH_SIZE / UNIFORM_FLASH_SECTOR_SIZE;
771e8a1faeSThomas Huth     }
781e8a1faeSThomas Huth 
791e8a1faeSThomas Huth     /* XXX: Limitations of test harness. */
801e8a1faeSThomas Huth     assert(ret.bank_width == 2);
811e8a1faeSThomas Huth     return ret;
821e8a1faeSThomas Huth }
831e8a1faeSThomas Huth 
841e8a1faeSThomas Huth /*
851e8a1faeSThomas Huth  * Return a bit mask suitable for extracting the least significant
861e8a1faeSThomas Huth  * status/query response from an interleaved response.
871e8a1faeSThomas Huth  */
device_mask(const FlashConfig * c)881e8a1faeSThomas Huth static inline uint64_t device_mask(const FlashConfig *c)
891e8a1faeSThomas Huth {
901e8a1faeSThomas Huth     return (uint64_t)-1;
911e8a1faeSThomas Huth }
921e8a1faeSThomas Huth 
931e8a1faeSThomas Huth /*
941e8a1faeSThomas Huth  * Return a bit mask exactly as long as the bank_width.
951e8a1faeSThomas Huth  */
bank_mask(const FlashConfig * c)961e8a1faeSThomas Huth static inline uint64_t bank_mask(const FlashConfig *c)
971e8a1faeSThomas Huth {
981e8a1faeSThomas Huth     if (c->bank_width == 8) {
991e8a1faeSThomas Huth         return (uint64_t)-1;
1001e8a1faeSThomas Huth     }
1011e8a1faeSThomas Huth     return (1ULL << (c->bank_width * 8)) - 1ULL;
1021e8a1faeSThomas Huth }
1031e8a1faeSThomas Huth 
flash_write(const FlashConfig * c,uint64_t byte_addr,uint64_t data)1041e8a1faeSThomas Huth static inline void flash_write(const FlashConfig *c, uint64_t byte_addr,
1051e8a1faeSThomas Huth                                uint64_t data)
1061e8a1faeSThomas Huth {
1071e8a1faeSThomas Huth     /* Sanity check our tests. */
1081e8a1faeSThomas Huth     assert((data & ~bank_mask(c)) == 0);
1091e8a1faeSThomas Huth     uint64_t addr = BASE_ADDR + byte_addr;
1101e8a1faeSThomas Huth     switch (c->bank_width) {
1111e8a1faeSThomas Huth     case 1:
1121e8a1faeSThomas Huth         qtest_writeb(c->qtest, addr, data);
1131e8a1faeSThomas Huth         break;
1141e8a1faeSThomas Huth     case 2:
1151e8a1faeSThomas Huth         qtest_writew(c->qtest, addr, data);
1161e8a1faeSThomas Huth         break;
1171e8a1faeSThomas Huth     case 4:
1181e8a1faeSThomas Huth         qtest_writel(c->qtest, addr, data);
1191e8a1faeSThomas Huth         break;
1201e8a1faeSThomas Huth     case 8:
1211e8a1faeSThomas Huth         qtest_writeq(c->qtest, addr, data);
1221e8a1faeSThomas Huth         break;
1231e8a1faeSThomas Huth     default:
1241e8a1faeSThomas Huth         abort();
1251e8a1faeSThomas Huth     }
1261e8a1faeSThomas Huth }
1271e8a1faeSThomas Huth 
flash_read(const FlashConfig * c,uint64_t byte_addr)1281e8a1faeSThomas Huth static inline uint64_t flash_read(const FlashConfig *c, uint64_t byte_addr)
1291e8a1faeSThomas Huth {
1301e8a1faeSThomas Huth     uint64_t addr = BASE_ADDR + byte_addr;
1311e8a1faeSThomas Huth     switch (c->bank_width) {
1321e8a1faeSThomas Huth     case 1:
1331e8a1faeSThomas Huth         return qtest_readb(c->qtest, addr);
1341e8a1faeSThomas Huth     case 2:
1351e8a1faeSThomas Huth         return qtest_readw(c->qtest, addr);
1361e8a1faeSThomas Huth     case 4:
1371e8a1faeSThomas Huth         return qtest_readl(c->qtest, addr);
1381e8a1faeSThomas Huth     case 8:
1391e8a1faeSThomas Huth         return qtest_readq(c->qtest, addr);
1401e8a1faeSThomas Huth     default:
1411e8a1faeSThomas Huth         abort();
1421e8a1faeSThomas Huth     }
1431e8a1faeSThomas Huth }
1441e8a1faeSThomas Huth 
1451e8a1faeSThomas Huth /*
1461e8a1faeSThomas Huth  * Convert a flash address expressed in the maximum width of the device as a
1471e8a1faeSThomas Huth  * byte address.
1481e8a1faeSThomas Huth  */
as_byte_addr(const FlashConfig * c,faddr flash_addr)1491e8a1faeSThomas Huth static inline uint64_t as_byte_addr(const FlashConfig *c, faddr flash_addr)
1501e8a1faeSThomas Huth {
1511e8a1faeSThomas Huth     /*
1521e8a1faeSThomas Huth      * Command addresses are always given as addresses in the maximum
1531e8a1faeSThomas Huth      * supported bus size for the flash chip. So an x8/x16 chip in x8 mode
1541e8a1faeSThomas Huth      * uses addresses 0xAAA and 0x555 to unlock because the least significant
1551e8a1faeSThomas Huth      * bit is ignored. (0x555 rather than 0x554 is traditional.)
1561e8a1faeSThomas Huth      *
1571e8a1faeSThomas Huth      * In general we need to multiply by the maximum device width.
1581e8a1faeSThomas Huth      */
1591e8a1faeSThomas Huth     return flash_addr.addr * c->bank_width;
1601e8a1faeSThomas Huth }
1611e8a1faeSThomas Huth 
1621e8a1faeSThomas Huth /*
1631e8a1faeSThomas Huth  * Return the command value or expected status replicated across all devices.
1641e8a1faeSThomas Huth  */
replicate(const FlashConfig * c,uint64_t data)1651e8a1faeSThomas Huth static inline uint64_t replicate(const FlashConfig *c, uint64_t data)
1661e8a1faeSThomas Huth {
1671e8a1faeSThomas Huth     /* Sanity check our tests. */
1681e8a1faeSThomas Huth     assert((data & ~device_mask(c)) == 0);
1691e8a1faeSThomas Huth     return data;
1701e8a1faeSThomas Huth }
1711e8a1faeSThomas Huth 
flash_cmd(const FlashConfig * c,faddr cmd_addr,uint8_t cmd)1721e8a1faeSThomas Huth static inline void flash_cmd(const FlashConfig *c, faddr cmd_addr,
1731e8a1faeSThomas Huth                              uint8_t cmd)
1741e8a1faeSThomas Huth {
1751e8a1faeSThomas Huth     flash_write(c, as_byte_addr(c, cmd_addr), replicate(c, cmd));
1761e8a1faeSThomas Huth }
1771e8a1faeSThomas Huth 
flash_query(const FlashConfig * c,faddr query_addr)1781e8a1faeSThomas Huth static inline uint64_t flash_query(const FlashConfig *c, faddr query_addr)
1791e8a1faeSThomas Huth {
1801e8a1faeSThomas Huth     return flash_read(c, as_byte_addr(c, query_addr));
1811e8a1faeSThomas Huth }
1821e8a1faeSThomas Huth 
flash_query_1(const FlashConfig * c,faddr query_addr)1831e8a1faeSThomas Huth static inline uint64_t flash_query_1(const FlashConfig *c, faddr query_addr)
1841e8a1faeSThomas Huth {
1851e8a1faeSThomas Huth     return flash_query(c, query_addr) & device_mask(c);
1861e8a1faeSThomas Huth }
1871e8a1faeSThomas Huth 
unlock(const FlashConfig * c)1881e8a1faeSThomas Huth static void unlock(const FlashConfig *c)
1891e8a1faeSThomas Huth {
1901e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, UNLOCK0_CMD);
1911e8a1faeSThomas Huth     flash_cmd(c, UNLOCK1_ADDR, UNLOCK1_CMD);
1921e8a1faeSThomas Huth }
1931e8a1faeSThomas Huth 
reset(const FlashConfig * c)1941e8a1faeSThomas Huth static void reset(const FlashConfig *c)
1951e8a1faeSThomas Huth {
1961e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0), RESET_CMD);
1971e8a1faeSThomas Huth }
1981e8a1faeSThomas Huth 
sector_erase(const FlashConfig * c,uint64_t byte_addr)1991e8a1faeSThomas Huth static void sector_erase(const FlashConfig *c, uint64_t byte_addr)
2001e8a1faeSThomas Huth {
2011e8a1faeSThomas Huth     unlock(c);
2021e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
2031e8a1faeSThomas Huth     unlock(c);
2041e8a1faeSThomas Huth     flash_write(c, byte_addr, replicate(c, SECTOR_ERASE_CMD));
2051e8a1faeSThomas Huth }
2061e8a1faeSThomas Huth 
wait_for_completion(const FlashConfig * c,uint64_t byte_addr)2071e8a1faeSThomas Huth static void wait_for_completion(const FlashConfig *c, uint64_t byte_addr)
2081e8a1faeSThomas Huth {
2091e8a1faeSThomas Huth     /* If DQ6 is toggling, step the clock and ensure the toggle stops. */
2101e8a1faeSThomas Huth     const uint64_t dq6 = replicate(c, 0x40);
2111e8a1faeSThomas Huth     if ((flash_read(c, byte_addr) & dq6) ^ (flash_read(c, byte_addr) & dq6)) {
2121e8a1faeSThomas Huth         /* Wait for erase or program to finish. */
2131e8a1faeSThomas Huth         qtest_clock_step_next(c->qtest);
2141e8a1faeSThomas Huth         /* Ensure that DQ6 has stopped toggling. */
2151e8a1faeSThomas Huth         g_assert_cmphex(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
2161e8a1faeSThomas Huth     }
2171e8a1faeSThomas Huth }
2181e8a1faeSThomas Huth 
bypass_program(const FlashConfig * c,uint64_t byte_addr,uint16_t data)2191e8a1faeSThomas Huth static void bypass_program(const FlashConfig *c, uint64_t byte_addr,
2201e8a1faeSThomas Huth                            uint16_t data)
2211e8a1faeSThomas Huth {
2221e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, PROGRAM_CMD);
2231e8a1faeSThomas Huth     flash_write(c, byte_addr, data);
2241e8a1faeSThomas Huth     /*
2251e8a1faeSThomas Huth      * Data isn't valid until DQ6 stops toggling. We don't model this as
2261e8a1faeSThomas Huth      * writes are immediate, but if this changes in the future, we can wait
2271e8a1faeSThomas Huth      * until the program is complete.
2281e8a1faeSThomas Huth      */
2291e8a1faeSThomas Huth     wait_for_completion(c, byte_addr);
2301e8a1faeSThomas Huth }
2311e8a1faeSThomas Huth 
program(const FlashConfig * c,uint64_t byte_addr,uint16_t data)2321e8a1faeSThomas Huth static void program(const FlashConfig *c, uint64_t byte_addr, uint16_t data)
2331e8a1faeSThomas Huth {
2341e8a1faeSThomas Huth     unlock(c);
2351e8a1faeSThomas Huth     bypass_program(c, byte_addr, data);
2361e8a1faeSThomas Huth }
2371e8a1faeSThomas Huth 
chip_erase(const FlashConfig * c)2381e8a1faeSThomas Huth static void chip_erase(const FlashConfig *c)
2391e8a1faeSThomas Huth {
2401e8a1faeSThomas Huth     unlock(c);
2411e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
2421e8a1faeSThomas Huth     unlock(c);
2431e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, CHIP_ERASE_CMD);
2441e8a1faeSThomas Huth }
2451e8a1faeSThomas Huth 
erase_suspend(const FlashConfig * c)2461e8a1faeSThomas Huth static void erase_suspend(const FlashConfig *c)
2471e8a1faeSThomas Huth {
2481e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0), ERASE_SUSPEND_CMD);
2491e8a1faeSThomas Huth }
2501e8a1faeSThomas Huth 
erase_resume(const FlashConfig * c)2511e8a1faeSThomas Huth static void erase_resume(const FlashConfig *c)
2521e8a1faeSThomas Huth {
2531e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0), ERASE_RESUME_CMD);
2541e8a1faeSThomas Huth }
2551e8a1faeSThomas Huth 
2561e8a1faeSThomas Huth /*
2571e8a1faeSThomas Huth  * Test flash commands with a variety of device geometry.
2581e8a1faeSThomas Huth  */
test_geometry(const void * opaque)2591e8a1faeSThomas Huth static void test_geometry(const void *opaque)
2601e8a1faeSThomas Huth {
2611e8a1faeSThomas Huth     const FlashConfig *config = opaque;
2621e8a1faeSThomas Huth     QTestState *qtest;
2631e8a1faeSThomas Huth     qtest = qtest_initf("-M musicpal"
264991c180dSPaolo Bonzini                         " -drive if=pflash,file=%s,format=raw,copy-on-read=on"
2651e8a1faeSThomas Huth                         /* Device geometry properties. */
2661e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2671e8a1faeSThomas Huth                         "property=num-blocks0,value=%d"
2681e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2691e8a1faeSThomas Huth                         "property=sector-length0,value=%d"
2701e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2711e8a1faeSThomas Huth                         "property=num-blocks1,value=%d"
2721e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2731e8a1faeSThomas Huth                         "property=sector-length1,value=%d"
2741e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2751e8a1faeSThomas Huth                         "property=num-blocks2,value=%d"
2761e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2771e8a1faeSThomas Huth                         "property=sector-length2,value=%d"
2781e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2791e8a1faeSThomas Huth                         "property=num-blocks3,value=%d"
2801e8a1faeSThomas Huth                         " -global driver=cfi.pflash02,"
2811e8a1faeSThomas Huth                         "property=sector-length3,value=%d",
2821e8a1faeSThomas Huth                         image_path,
2831e8a1faeSThomas Huth                         config->nb_blocs[0],
2841e8a1faeSThomas Huth                         config->sector_len[0],
2851e8a1faeSThomas Huth                         config->nb_blocs[1],
2861e8a1faeSThomas Huth                         config->sector_len[1],
2871e8a1faeSThomas Huth                         config->nb_blocs[2],
2881e8a1faeSThomas Huth                         config->sector_len[2],
2891e8a1faeSThomas Huth                         config->nb_blocs[3],
2901e8a1faeSThomas Huth                         config->sector_len[3]);
2911e8a1faeSThomas Huth     FlashConfig explicit_config = expand_config_defaults(config);
2921e8a1faeSThomas Huth     explicit_config.qtest = qtest;
2931e8a1faeSThomas Huth     const FlashConfig *c = &explicit_config;
2941e8a1faeSThomas Huth 
2951e8a1faeSThomas Huth     /* Check the IDs. */
2961e8a1faeSThomas Huth     unlock(c);
2971e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD);
2981e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
2991e8a1faeSThomas Huth     if (c->bank_width >= 2) {
3001e8a1faeSThomas Huth         /*
3011e8a1faeSThomas Huth          * XXX: The ID returned by the musicpal flash chip is 16 bits which
3021e8a1faeSThomas Huth          * wouldn't happen with an 8-bit device. It would probably be best to
3031e8a1faeSThomas Huth          * prohibit addresses larger than the device width in pflash_cfi02.c,
3041e8a1faeSThomas Huth          * but then we couldn't test smaller device widths at all.
3051e8a1faeSThomas Huth          */
3061e8a1faeSThomas Huth         g_assert_cmphex(flash_query(c, FLASH_ADDR(1)), ==,
3071e8a1faeSThomas Huth                         replicate(c, 0x236D));
3081e8a1faeSThomas Huth     }
3091e8a1faeSThomas Huth     reset(c);
3101e8a1faeSThomas Huth 
3111e8a1faeSThomas Huth     /* Check the erase blocks. */
3121e8a1faeSThomas Huth     flash_cmd(c, CFI_ADDR, CFI_CMD);
3131e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q'));
3141e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R'));
3151e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y'));
3161e8a1faeSThomas Huth 
3171e8a1faeSThomas Huth     /* Num erase regions. */
3181e8a1faeSThomas Huth     int nb_erase_regions = flash_query_1(c, FLASH_ADDR(0x2C));
3191e8a1faeSThomas Huth     g_assert_cmphex(nb_erase_regions, ==,
3201e8a1faeSThomas Huth                     !!c->nb_blocs[0] + !!c->nb_blocs[1] + !!c->nb_blocs[2] +
3211e8a1faeSThomas Huth                     !!c->nb_blocs[3]);
3221e8a1faeSThomas Huth 
3231e8a1faeSThomas Huth     /* Check device length. */
3241e8a1faeSThomas Huth     uint32_t device_len = 1 << flash_query_1(c, FLASH_ADDR(0x27));
3251e8a1faeSThomas Huth     g_assert_cmphex(device_len, ==, UNIFORM_FLASH_SIZE);
3261e8a1faeSThomas Huth 
3271e8a1faeSThomas Huth     /* Check that erase suspend to read/write is supported. */
3281e8a1faeSThomas Huth     uint16_t pri = flash_query_1(c, FLASH_ADDR(0x15)) +
3291e8a1faeSThomas Huth                    (flash_query_1(c, FLASH_ADDR(0x16)) << 8);
3301e8a1faeSThomas Huth     g_assert_cmpint(pri, >=, 0x2D + 4 * nb_erase_regions);
3311e8a1faeSThomas Huth     g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 0)), ==, replicate(c, 'P'));
3321e8a1faeSThomas Huth     g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 1)), ==, replicate(c, 'R'));
3331e8a1faeSThomas Huth     g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 2)), ==, replicate(c, 'I'));
3341e8a1faeSThomas Huth     g_assert_cmpint(flash_query_1(c, FLASH_ADDR(pri + 6)), ==, 2); /* R/W */
3351e8a1faeSThomas Huth     reset(c);
3361e8a1faeSThomas Huth 
3371e8a1faeSThomas Huth     const uint64_t dq7 = replicate(c, 0x80);
3381e8a1faeSThomas Huth     const uint64_t dq6 = replicate(c, 0x40);
3391e8a1faeSThomas Huth     const uint64_t dq3 = replicate(c, 0x08);
3401e8a1faeSThomas Huth     const uint64_t dq2 = replicate(c, 0x04);
3411e8a1faeSThomas Huth 
3421e8a1faeSThomas Huth     uint64_t byte_addr = 0;
3431e8a1faeSThomas Huth     for (int region = 0; region < nb_erase_regions; ++region) {
3441e8a1faeSThomas Huth         uint64_t base = 0x2D + 4 * region;
3451e8a1faeSThomas Huth         flash_cmd(c, CFI_ADDR, CFI_CMD);
3461e8a1faeSThomas Huth         uint32_t nb_sectors = flash_query_1(c, FLASH_ADDR(base + 0)) +
3471e8a1faeSThomas Huth                               (flash_query_1(c, FLASH_ADDR(base + 1)) << 8) + 1;
3481e8a1faeSThomas Huth         uint32_t sector_len = (flash_query_1(c, FLASH_ADDR(base + 2)) << 8) +
3491e8a1faeSThomas Huth                               (flash_query_1(c, FLASH_ADDR(base + 3)) << 16);
3501e8a1faeSThomas Huth         g_assert_cmphex(nb_sectors, ==, c->nb_blocs[region]);
3511e8a1faeSThomas Huth         g_assert_cmphex(sector_len, ==, c->sector_len[region]);
3521e8a1faeSThomas Huth         reset(c);
3531e8a1faeSThomas Huth 
3541e8a1faeSThomas Huth         /* Erase and program sector. */
3551e8a1faeSThomas Huth         for (uint32_t i = 0; i < nb_sectors; ++i) {
3561e8a1faeSThomas Huth             sector_erase(c, byte_addr);
3571e8a1faeSThomas Huth 
3581e8a1faeSThomas Huth             /* Check that DQ3 is 0. */
3591e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr) & dq3, ==, 0);
3601e8a1faeSThomas Huth             qtest_clock_step_next(c->qtest); /* Step over the 50 us timeout. */
3611e8a1faeSThomas Huth 
3621e8a1faeSThomas Huth             /* Check that DQ3 is 1. */
3631e8a1faeSThomas Huth             uint64_t status0 = flash_read(c, byte_addr);
3641e8a1faeSThomas Huth             g_assert_cmphex(status0 & dq3, ==, dq3);
3651e8a1faeSThomas Huth 
3661e8a1faeSThomas Huth             /* DQ7 is 0 during an erase. */
3671e8a1faeSThomas Huth             g_assert_cmphex(status0 & dq7, ==, 0);
3681e8a1faeSThomas Huth             uint64_t status1 = flash_read(c, byte_addr);
3691e8a1faeSThomas Huth 
3701e8a1faeSThomas Huth             /* DQ6 toggles during an erase. */
3711e8a1faeSThomas Huth             g_assert_cmphex(status0 & dq6, ==, ~status1 & dq6);
3721e8a1faeSThomas Huth 
3731e8a1faeSThomas Huth             /* Wait for erase to complete. */
3741e8a1faeSThomas Huth             wait_for_completion(c, byte_addr);
3751e8a1faeSThomas Huth 
3761e8a1faeSThomas Huth             /* Ensure DQ6 has stopped toggling. */
3771e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr), ==,
3781e8a1faeSThomas Huth                             flash_read(c, byte_addr));
3791e8a1faeSThomas Huth 
3801e8a1faeSThomas Huth             /* Now the data should be valid. */
3811e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
3821e8a1faeSThomas Huth 
3831e8a1faeSThomas Huth             /* Program a bit pattern. */
3841e8a1faeSThomas Huth             program(c, byte_addr, 0x55);
3851e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr) & 0xFF, ==, 0x55);
3861e8a1faeSThomas Huth             program(c, byte_addr, 0xA5);
3871e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr) & 0xFF, ==, 0x05);
3881e8a1faeSThomas Huth             byte_addr += sector_len;
3891e8a1faeSThomas Huth         }
3901e8a1faeSThomas Huth     }
3911e8a1faeSThomas Huth 
3921e8a1faeSThomas Huth     /* Erase the chip. */
3931e8a1faeSThomas Huth     chip_erase(c);
3941e8a1faeSThomas Huth     /* Read toggle. */
3951e8a1faeSThomas Huth     uint64_t status0 = flash_read(c, 0);
3961e8a1faeSThomas Huth     /* DQ7 is 0 during an erase. */
3971e8a1faeSThomas Huth     g_assert_cmphex(status0 & dq7, ==, 0);
3981e8a1faeSThomas Huth     uint64_t status1 = flash_read(c, 0);
3991e8a1faeSThomas Huth     /* DQ6 toggles during an erase. */
4001e8a1faeSThomas Huth     g_assert_cmphex(status0 & dq6, ==, ~status1 & dq6);
4011e8a1faeSThomas Huth     /* Wait for erase to complete. */
4021e8a1faeSThomas Huth     qtest_clock_step_next(c->qtest);
4031e8a1faeSThomas Huth     /* Ensure DQ6 has stopped toggling. */
4041e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 0), ==, flash_read(c, 0));
4051e8a1faeSThomas Huth     /* Now the data should be valid. */
4061e8a1faeSThomas Huth 
4071e8a1faeSThomas Huth     for (int region = 0; region < nb_erase_regions; ++region) {
4081e8a1faeSThomas Huth         for (uint32_t i = 0; i < c->nb_blocs[region]; ++i) {
409*82fdcd3eSPhilippe Mathieu-Daudé             byte_addr = (uint64_t)i * c->sector_len[region];
4101e8a1faeSThomas Huth             g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
4111e8a1faeSThomas Huth         }
4121e8a1faeSThomas Huth     }
4131e8a1faeSThomas Huth 
4141e8a1faeSThomas Huth     /* Unlock bypass */
4151e8a1faeSThomas Huth     unlock(c);
4161e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, UNLOCK_BYPASS_CMD);
4171e8a1faeSThomas Huth     bypass_program(c, 0 * c->bank_width, 0x01);
4181e8a1faeSThomas Huth     bypass_program(c, 1 * c->bank_width, 0x23);
4191e8a1faeSThomas Huth     bypass_program(c, 2 * c->bank_width, 0x45);
4201e8a1faeSThomas Huth     /*
4211e8a1faeSThomas Huth      * Test that bypass programming, unlike normal programming can use any
4221e8a1faeSThomas Huth      * address for the PROGRAM_CMD.
4231e8a1faeSThomas Huth      */
4241e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(3 * c->bank_width), PROGRAM_CMD);
4251e8a1faeSThomas Huth     flash_write(c, 3 * c->bank_width, 0x67);
4261e8a1faeSThomas Huth     wait_for_completion(c, 3 * c->bank_width);
4271e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0), UNLOCK_BYPASS_RESET_CMD);
4281e8a1faeSThomas Huth     bypass_program(c, 4 * c->bank_width, 0x89); /* Should fail. */
4291e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 0 * c->bank_width), ==, 0x01);
4301e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 1 * c->bank_width), ==, 0x23);
4311e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 2 * c->bank_width), ==, 0x45);
4321e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 3 * c->bank_width), ==, 0x67);
4331e8a1faeSThomas Huth     g_assert_cmphex(flash_read(c, 4 * c->bank_width), ==, bank_mask(c));
4341e8a1faeSThomas Huth 
4351e8a1faeSThomas Huth     /* Test ignored high order bits of address. */
4361e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0x5555), UNLOCK0_CMD);
4371e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0x2AAA), UNLOCK1_CMD);
4381e8a1faeSThomas Huth     flash_cmd(c, FLASH_ADDR(0x5555), AUTOSELECT_CMD);
4391e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
4401e8a1faeSThomas Huth     reset(c);
4411e8a1faeSThomas Huth 
4421e8a1faeSThomas Huth     /*
4431e8a1faeSThomas Huth      * Program a word on each sector, erase one or two sectors per region, and
4441e8a1faeSThomas Huth      * verify that all of those, and only those, are erased.
4451e8a1faeSThomas Huth      */
4461e8a1faeSThomas Huth     byte_addr = 0;
4471e8a1faeSThomas Huth     for (int region = 0; region < nb_erase_regions; ++region) {
4481e8a1faeSThomas Huth         for (int i = 0; i < config->nb_blocs[region]; ++i) {
4491e8a1faeSThomas Huth             program(c, byte_addr, 0);
4501e8a1faeSThomas Huth             byte_addr += config->sector_len[region];
4511e8a1faeSThomas Huth         }
4521e8a1faeSThomas Huth     }
4531e8a1faeSThomas Huth     unlock(c);
4541e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
4551e8a1faeSThomas Huth     unlock(c);
4561e8a1faeSThomas Huth     byte_addr = 0;
4571e8a1faeSThomas Huth     const uint64_t erase_cmd = replicate(c, SECTOR_ERASE_CMD);
4581e8a1faeSThomas Huth     for (int region = 0; region < nb_erase_regions; ++region) {
4591e8a1faeSThomas Huth         flash_write(c, byte_addr, erase_cmd);
4601e8a1faeSThomas Huth         if (c->nb_blocs[region] > 1) {
4611e8a1faeSThomas Huth             flash_write(c, byte_addr + c->sector_len[region], erase_cmd);
4621e8a1faeSThomas Huth         }
4631e8a1faeSThomas Huth         byte_addr += c->sector_len[region] * c->nb_blocs[region];
4641e8a1faeSThomas Huth     }
4651e8a1faeSThomas Huth 
4661e8a1faeSThomas Huth     qtest_clock_step_next(c->qtest); /* Step over the 50 us timeout. */
4671e8a1faeSThomas Huth     wait_for_completion(c, 0);
4681e8a1faeSThomas Huth     byte_addr = 0;
4691e8a1faeSThomas Huth     for (int region = 0; region < nb_erase_regions; ++region) {
4701e8a1faeSThomas Huth         for (int i = 0; i < config->nb_blocs[region]; ++i) {
4711e8a1faeSThomas Huth             if (i < 2) {
4721e8a1faeSThomas Huth                 g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
4731e8a1faeSThomas Huth             } else {
4741e8a1faeSThomas Huth                 g_assert_cmphex(flash_read(c, byte_addr), ==, 0);
4751e8a1faeSThomas Huth             }
4761e8a1faeSThomas Huth             byte_addr += config->sector_len[region];
4771e8a1faeSThomas Huth         }
4781e8a1faeSThomas Huth     }
4791e8a1faeSThomas Huth 
4801e8a1faeSThomas Huth     /* Test erase suspend/resume during erase timeout. */
4811e8a1faeSThomas Huth     sector_erase(c, 0);
4821e8a1faeSThomas Huth     /*
4831e8a1faeSThomas Huth      * Check that DQ 3 is 0 and DQ6 and DQ2 are toggling in the sector being
4841e8a1faeSThomas Huth      * erased as well as in a sector not being erased.
4851e8a1faeSThomas Huth      */
4861e8a1faeSThomas Huth     byte_addr = c->sector_len[0];
4871e8a1faeSThomas Huth     status0 = flash_read(c, 0);
4881e8a1faeSThomas Huth     status1 = flash_read(c, 0);
4891e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, 0);
4901e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
4911e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
4921e8a1faeSThomas Huth     status0 = flash_read(c, byte_addr);
4931e8a1faeSThomas Huth     status1 = flash_read(c, byte_addr);
4941e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, 0);
4951e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
4961e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
4971e8a1faeSThomas Huth 
4981e8a1faeSThomas Huth     /*
4991e8a1faeSThomas Huth      * Check that after suspending, DQ6 does not toggle but DQ2 does toggle in
5001e8a1faeSThomas Huth      * an erase suspended sector but that neither toggle (we should be
5011e8a1faeSThomas Huth      * getting data) in a sector not being erased.
5021e8a1faeSThomas Huth      */
5031e8a1faeSThomas Huth     erase_suspend(c);
5041e8a1faeSThomas Huth     status0 = flash_read(c, 0);
5051e8a1faeSThomas Huth     status1 = flash_read(c, 0);
5061e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, status1 & dq6);
5071e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5081e8a1faeSThomas Huth     g_assert_cmpint(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
5091e8a1faeSThomas Huth 
5101e8a1faeSThomas Huth     /* Check that after resuming, DQ3 is 1 and DQ6 and DQ2 toggle. */
5111e8a1faeSThomas Huth     erase_resume(c);
5121e8a1faeSThomas Huth     status0 = flash_read(c, 0);
5131e8a1faeSThomas Huth     status1 = flash_read(c, 0);
5141e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5151e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5161e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5171e8a1faeSThomas Huth     status0 = flash_read(c, byte_addr);
5181e8a1faeSThomas Huth     status1 = flash_read(c, byte_addr);
5191e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5201e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5211e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5221e8a1faeSThomas Huth     wait_for_completion(c, 0);
5231e8a1faeSThomas Huth 
5241e8a1faeSThomas Huth     /* Repeat this process but this time suspend after the timeout. */
5251e8a1faeSThomas Huth     sector_erase(c, 0);
5261e8a1faeSThomas Huth     qtest_clock_step_next(c->qtest);
5271e8a1faeSThomas Huth     /*
5281e8a1faeSThomas Huth      * Check that DQ 3 is 1 and DQ6 and DQ2 are toggling in the sector being
5291e8a1faeSThomas Huth      * erased as well as in a sector not being erased.
5301e8a1faeSThomas Huth      */
5311e8a1faeSThomas Huth     byte_addr = c->sector_len[0];
5321e8a1faeSThomas Huth     status0 = flash_read(c, 0);
5331e8a1faeSThomas Huth     status1 = flash_read(c, 0);
5341e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5351e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5361e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5371e8a1faeSThomas Huth     status0 = flash_read(c, byte_addr);
5381e8a1faeSThomas Huth     status1 = flash_read(c, byte_addr);
5391e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5401e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5411e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5421e8a1faeSThomas Huth 
5431e8a1faeSThomas Huth     /*
5441e8a1faeSThomas Huth      * Check that after suspending, DQ6 does not toggle but DQ2 does toggle in
5451e8a1faeSThomas Huth      * an erase suspended sector but that neither toggle (we should be
5461e8a1faeSThomas Huth      * getting data) in a sector not being erased.
5471e8a1faeSThomas Huth      */
5481e8a1faeSThomas Huth     erase_suspend(c);
5491e8a1faeSThomas Huth     status0 = flash_read(c, 0);
5501e8a1faeSThomas Huth     status1 = flash_read(c, 0);
5511e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, status1 & dq6);
5521e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5531e8a1faeSThomas Huth     g_assert_cmpint(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
5541e8a1faeSThomas Huth 
5551e8a1faeSThomas Huth     /* Check that after resuming, DQ3 is 1 and DQ6 and DQ2 toggle. */
5561e8a1faeSThomas Huth     erase_resume(c);
5571e8a1faeSThomas Huth     status0 = flash_read(c, 0);
5581e8a1faeSThomas Huth     status1 = flash_read(c, 0);
5591e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5601e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5611e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5621e8a1faeSThomas Huth     status0 = flash_read(c, byte_addr);
5631e8a1faeSThomas Huth     status1 = flash_read(c, byte_addr);
5641e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq3, ==, dq3);
5651e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
5661e8a1faeSThomas Huth     g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
5671e8a1faeSThomas Huth     wait_for_completion(c, 0);
5681e8a1faeSThomas Huth 
5691e8a1faeSThomas Huth     qtest_quit(qtest);
5701e8a1faeSThomas Huth }
5711e8a1faeSThomas Huth 
5721e8a1faeSThomas Huth /*
5731e8a1faeSThomas Huth  * Test that
5741e8a1faeSThomas Huth  * 1. enter autoselect mode;
5751e8a1faeSThomas Huth  * 2. enter CFI mode; and then
5761e8a1faeSThomas Huth  * 3. exit CFI mode
5771e8a1faeSThomas Huth  * leaves the flash device in autoselect mode.
5781e8a1faeSThomas Huth  */
test_cfi_in_autoselect(const void * opaque)5791e8a1faeSThomas Huth static void test_cfi_in_autoselect(const void *opaque)
5801e8a1faeSThomas Huth {
5811e8a1faeSThomas Huth     const FlashConfig *config = opaque;
5821e8a1faeSThomas Huth     QTestState *qtest;
5831e8a1faeSThomas Huth     qtest = qtest_initf("-M musicpal"
584991c180dSPaolo Bonzini                         " -drive if=pflash,file=%s,format=raw,copy-on-read=on",
5851e8a1faeSThomas Huth                         image_path);
5861e8a1faeSThomas Huth     FlashConfig explicit_config = expand_config_defaults(config);
5871e8a1faeSThomas Huth     explicit_config.qtest = qtest;
5881e8a1faeSThomas Huth     const FlashConfig *c = &explicit_config;
5891e8a1faeSThomas Huth 
5901e8a1faeSThomas Huth     /* 1. Enter autoselect. */
5911e8a1faeSThomas Huth     unlock(c);
5921e8a1faeSThomas Huth     flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD);
5931e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
5941e8a1faeSThomas Huth 
5951e8a1faeSThomas Huth     /* 2. Enter CFI. */
5961e8a1faeSThomas Huth     flash_cmd(c, CFI_ADDR, CFI_CMD);
5971e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q'));
5981e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R'));
5991e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y'));
6001e8a1faeSThomas Huth 
6011e8a1faeSThomas Huth     /* 3. Exit CFI. */
6021e8a1faeSThomas Huth     reset(c);
6031e8a1faeSThomas Huth     g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
6041e8a1faeSThomas Huth 
6051e8a1faeSThomas Huth     qtest_quit(qtest);
6061e8a1faeSThomas Huth }
6071e8a1faeSThomas Huth 
cleanup(void * opaque)6081e8a1faeSThomas Huth static void cleanup(void *opaque)
6091e8a1faeSThomas Huth {
6101e8a1faeSThomas Huth     unlink(image_path);
6118189b27dSBin Meng     g_free(image_path);
6121e8a1faeSThomas Huth }
6131e8a1faeSThomas Huth 
6141e8a1faeSThomas Huth /*
6151e8a1faeSThomas Huth  * XXX: Tests are limited to bank_width = 2 for now because that's what
6161e8a1faeSThomas Huth  * hw/arm/musicpal.c has.
6171e8a1faeSThomas Huth  */
6181e8a1faeSThomas Huth static const FlashConfig configuration[] = {
6191e8a1faeSThomas Huth     /* One x16 device. */
6201e8a1faeSThomas Huth     {
6211e8a1faeSThomas Huth         .bank_width = 2,
6221e8a1faeSThomas Huth     },
6231e8a1faeSThomas Huth     /* Nonuniform sectors (top boot). */
6241e8a1faeSThomas Huth     {
6251e8a1faeSThomas Huth         .bank_width = 2,
6261e8a1faeSThomas Huth         .nb_blocs = { 127, 1, 2, 1 },
6271e8a1faeSThomas Huth         .sector_len = { 0x10000, 0x08000, 0x02000, 0x04000 },
6281e8a1faeSThomas Huth     },
6291e8a1faeSThomas Huth     /* Nonuniform sectors (bottom boot). */
6301e8a1faeSThomas Huth     {
6311e8a1faeSThomas Huth         .bank_width = 2,
6321e8a1faeSThomas Huth         .nb_blocs = { 1, 2, 1, 127 },
6331e8a1faeSThomas Huth         .sector_len = { 0x04000, 0x02000, 0x08000, 0x10000 },
6341e8a1faeSThomas Huth     },
6351e8a1faeSThomas Huth };
6361e8a1faeSThomas Huth 
main(int argc,char ** argv)6371e8a1faeSThomas Huth int main(int argc, char **argv)
6381e8a1faeSThomas Huth {
6398189b27dSBin Meng     GError *err = NULL;
6408189b27dSBin Meng     int fd = g_file_open_tmp("qtest.XXXXXX", &image_path, &err);
6418189b27dSBin Meng     g_assert_no_error(err);
6428189b27dSBin Meng 
6431e8a1faeSThomas Huth     if (ftruncate(fd, UNIFORM_FLASH_SIZE) < 0) {
6441e8a1faeSThomas Huth         int error_code = errno;
6451e8a1faeSThomas Huth         close(fd);
6468189b27dSBin Meng         cleanup(NULL);
6471e8a1faeSThomas Huth         g_printerr("Failed to truncate file %s to %u MB: %s\n", image_path,
6481e8a1faeSThomas Huth                    UNIFORM_FLASH_SIZE, strerror(error_code));
6491e8a1faeSThomas Huth         exit(EXIT_FAILURE);
6501e8a1faeSThomas Huth     }
6511e8a1faeSThomas Huth     close(fd);
6521e8a1faeSThomas Huth 
6531e8a1faeSThomas Huth     qtest_add_abrt_handler(cleanup, NULL);
6541e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
6551e8a1faeSThomas Huth 
6561e8a1faeSThomas Huth     size_t nb_configurations = sizeof configuration / sizeof configuration[0];
6571e8a1faeSThomas Huth     for (size_t i = 0; i < nb_configurations; ++i) {
6581e8a1faeSThomas Huth         const FlashConfig *config = &configuration[i];
6591e8a1faeSThomas Huth         char *path = g_strdup_printf("pflash-cfi02"
6601e8a1faeSThomas Huth                                      "/geometry/%dx%x-%dx%x-%dx%x-%dx%x"
6611e8a1faeSThomas Huth                                      "/%d",
6621e8a1faeSThomas Huth                                      config->nb_blocs[0],
6631e8a1faeSThomas Huth                                      config->sector_len[0],
6641e8a1faeSThomas Huth                                      config->nb_blocs[1],
6651e8a1faeSThomas Huth                                      config->sector_len[1],
6661e8a1faeSThomas Huth                                      config->nb_blocs[2],
6671e8a1faeSThomas Huth                                      config->sector_len[2],
6681e8a1faeSThomas Huth                                      config->nb_blocs[3],
6691e8a1faeSThomas Huth                                      config->sector_len[3],
6701e8a1faeSThomas Huth                                      config->bank_width);
6711e8a1faeSThomas Huth         qtest_add_data_func(path, config, test_geometry);
6721e8a1faeSThomas Huth         g_free(path);
6731e8a1faeSThomas Huth     }
6741e8a1faeSThomas Huth 
6751e8a1faeSThomas Huth     qtest_add_data_func("pflash-cfi02/cfi-in-autoselect", &configuration[0],
6761e8a1faeSThomas Huth                         test_cfi_in_autoselect);
6771e8a1faeSThomas Huth     int result = g_test_run();
6781e8a1faeSThomas Huth     cleanup(NULL);
6791e8a1faeSThomas Huth     return result;
6801e8a1faeSThomas Huth }
681