2014-02-20 39 views
0

我正面临以下问题。big endian swap宏“uswap”导致意外错误

#define uswap_32(x) \ 
((((x) & 0xff000000) >> 24) | \ 
(((x) & 0x00ff0000) >> 8) | \ 
(((x) & 0x0000ff00) << 8) | \ 
(((x) & 0x000000ff) << 24)) 

获取的以下数字作为自变量x = 0x49074808

为什么我的程序中断/复位这里?

THX

编辑:

我真正的应用程序的说明:

我有一个引导程序坐在闪存起始地址0x08000000U下去,直到0x08004000U。 自举程序在flash中有一个uImage标头(取自uboot),大小为0x40。 在我的应用程序中,我只想检查一下,如果实际上有一个正确的uImage标头,因为我有两个bootloader版本。一个可以处理uImage类型的图像,另一个不能。在最后一种情况下,在bootloader应用程序之后根本没有uImage头文件,这里有应用程序代码!

在应用我只是想检查帧头CRC:

#define UIMAGE_FLASH_ADDRESS  (0x08004000U) 
image_header_t *header; 
header = (image_header_t *) UIMAGE_FLASH_ADDRESS; 
if (image_check_hcrc(header)) 
    /* do something...*/ 




static int image_check_hcrc(const image_header_t *hdr) 
{ 
    uint32_t hcrc; 
    uint32_t len = image_get_header_size(); 
    image_header_t header; 

    /* Copy header so we can blank CRC field for re-calculation */ 
    memcpy(&header, (char *)hdr, image_get_header_size()); 
    header.ih_hcrc = 0;   // byte order independent 
    hcrc = crc32(0, (unsigned char *)&header, len); 

    return hcrc == image_get_hcrc(hdr); 
} 

呼吁uswap_32()发生在上面的函数的最后一行:

#define uswap_32(x) \ 
((((x) & 0xff000000) >> 24) | \ 
(((x) & 0x00ff0000) >> 8) | \ 
(((x) & 0x0000ff00) << 8) | \ 
(((x) & 0x000000ff) << 24)) 

# define cpu_to_be32(x)  uswap_32(x) 
# define be32_to_cpu(x)  uswap_32(x) 
#define uimage_to_cpu(x)  be32_to_cpu(x) 
#define cpu_to_uimage(x)  cpu_to_be32(x) 



#define image_get_hdr_l(f) \ 
    static inline uint32_t image_get_##f(const image_header_t *hdr) \ 
{ \ 
    return uimage_to_cpu(hdr->ih_##f); \ 
} 
image_get_hdr_l(magic)  /* image_get_magic */ 
image_get_hdr_l(hcrc)  /* image_get_hcrc */ 
image_get_hdr_l(time)  /* image_get_time */ 
image_get_hdr_l(size)  /* image_get_size */ 
image_get_hdr_l(load)  /* image_get_load */ 
image_get_hdr_l(ep)  /* image_get_ep */ 
image_get_hdr_l(dcrc)  /* image_get_dcrc */ 

#define image_get_hdr_b(f) \ 
static inline uint8_t image_get_##f(const image_header_t *hdr) \ 
{ \ 
    return hdr->ih_##f; \ 
} 
image_get_hdr_b(os)  /* image_get_os */ 
image_get_hdr_b(arch)  /* image_get_arch */ 
image_get_hdr_b(type)  /* image_get_type */ 
image_get_hdr_b(comp)  /* image_get_comp */ 
+4

你可以说*如何*它“打破/重置”?预期的结果是什么?什么是实际结果?你如何使用这个宏(实际的代码)? –

+2

你的宏观看货物。分享你怎么称呼它 – MOHAMED

+0

我测试你的宏http://www.compileonline.com/compile_c_online.php和它的作品没有问题:'的main(){ unsigned int类型A = 0x45231512; 的printf( “%02X \ n” 个,uswap_32(A)); }' – MOHAMED

回答

2

这是个好主意将x分配给宏中的局部变量。否则,如果一个表达式作为参数传递给宏,它将被评估4次。例如,uswap(2 + 3),甚至更糟,uswap(some_func(x))。

第二个问题 - 你需要增加明确的UL类型修饰符常量。这里是一个更安全的宏版本:

#define uswap_32(x) ({\ 
    uint32_t _x = (x);\ 
    (uint32_t)(\ 
     ((_x & 0xff000000UL) >> 24) | \ 
     ((_x & 0x00ff0000UL) >> 8) | \ 
     ((_x & 0x0000ff00UL) << 8) | \ 
     ((_x & 0x000000ffUL) << 24)); \ 
})