Least significant byte (LSB) Most significant byte (MSB)

Big-endian machines store the most-significant byte at the lowest-numbered address, with the other bytes following in decreasing order of significance. Little-endian machines store multi-byte numbers in the opposite order: from least-significant to most-significant.

Internet diagram is big-endian.  STM32 ARM-CM3 is little-endian.

这是从wiki的Example

如何用简单的方法检查是大端还是小端

uint8_t IsBigEndian(void)
{
volatile uint32_t temp = 0x0a0b0c0d;
return (*((uint8_t*)(&temp))) == 0x0a;
} void EndianTest(void)
{
if (IsBigEndian() == )
{
std::cout << "it is a big-endian" << std::endl;
}
else
{
std::cout << "it is a little-endian" << std::endl; }
}

从调试也可以看出内存地址和值, 注意0x0a是most-significant byte。

字节顺序其实就是说在计算机内存中怎么存储,是说从左到右,还是从右到左。对于在一个机器内的数据交互,那种字节顺序都没有关系。主要的问题是在不同机器上交互,比如你设计一个总线系统,那么你就要充分考虑你的通讯协议使用大端还是小端。比如说TCP/IP协议它就是是按照big-endian来设计的,因为你不知道网络系统的电脑,路由器,等等的字节顺序,所以在设计通讯协议时就要充分考虑到这一点。

在TCP/IP里有一系列的函数用于字节的转换如htons (covert host byte to network byte short 16bit), htonl (convert host byte to network byte long 32bit), ntohs (network byte to host byte short 16bit), ntohl (convert network byte to host byte long 32bit). 当你设计通讯协议时,这些函数你就要自己实现了。

说到TCP/IP,让我想起一个我们以前遇到的一个问题,我们的panel在客户那里每个星期二凌晨都会自动重启。后来分析是由于客户每个星期二IT部门会用nmap进行扫描,而TCP/IP字节数的问题造成系统死了,后由Watchdog重启。我把以前分析的一些简要内容放在这里(只有英语版)。

Problem Description

The field reports show that panel will lockup and then reboot while doing security port scan by nmap.

Root Cause

After analysis, this problem happens at all panel versions, standalone/networking panel, and happens at other scanning tools (e.g. Tenable Nessus).

The investigation shows that, the problem was caused by unaligned data access during TCP timestamps option parsing in Linux kernel. The reason can be summarized below:

1)      The ARMv5 or earlier MCU (likes panel MCU S3C44B0X ARM7TDMI is belong to ARMv4T), who had limited abilities to access memory that was not aligned on a word (four byte) boundary. According to S3C44B0 datasheet (see page 89 Address Alignment), the MCU is going to ABORT mode which cause the system crash.

2)      In Linux TCP option parsing, the handling did not consider the data unaligned in timestamp option, but TCP options are not guaranteed to be aligned at all.

According to the Linux debug output information when TCP package with timestamps options: The timestamps hold address (register: R8) is 0x0DB074E2, which is not multiple 4(word), the Linux kernel error information is: Unhandled fault: alignment exception (13).

1)      TCP package by nmap

2)      Linux debug output

Solution

The solution is get word value byte one byte when the address is unaligned, which is come from official Linux kernel patch (see appendix). The change is only applied for TCP/IP option parsing and without side effect. The solution had been successfully verified by prototype.

Purpose: for field panels, firmware upgraded is the recommended solution if the panel was be connected to internet or corporate network, and it is not need to upgrade firmware if the panel was not be connected to network.

Appendix

The patch is come from Linux official, the link is https://archive.org/details/git-history-of-linux.

 

Big-endian/Little-endian, LSB/MSB的更多相关文章

  1. MSB与LSB Big Endian Little Endian

    Most Significant Bit, Last(Least) Significant Bit 最高有效位(MSB) 指二进制中最高值的比特.在16比特的数字音频中,其第1个比特便对16bit的字 ...

  2. 彻底搞懂字符编码(unicode,mbcs,utf-8,utf-16,utf-32,big endian,little endian...)[转]

    最近有一些朋友常问我一些乱码的问题,和他们交流过程中,发现这个编码的相关知识还真是杂乱不堪,不少人对一些知识理解似乎也有些偏差,网上百度, google的内容,也有不少以讹传讹,根本就是错误的(例如说 ...

  3. LSB MSB

    #LSB:(Least Significant,Bit) 最低有效位 :MSB(Most Significant Bit):最高有效位,若MSB=1,则表示数据为负值,若MSB=0则表示数据为正. 在 ...

  4. 低字节序和高字节序相互转换(Little Endian/Big Endian)

    这个例子展示了如何转换整形数字的字节顺序,该方法可以用来在little-endian和big-endian之间转换. 说明:Windos(x86,x64)和Linux(x86,x64)都是little ...

  5. 关于Big Endian 和 Little Endian

    Big Endian 和 Little Endian 一.字节序 来自:http://ayazh.gjjblog.com/archives/1058846/ 谈到字节序的问题,必然牵涉到两大CPU派系 ...

  6. 清晰讲解LSB、MSB和大小端模式及网络字节序

    时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...

  7. 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian

    转载:http://witmax.cn/character-encoding-notes.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问 ...

  8. 06socket编程

    socket可以看成是用户进程与内核网络协议栈的编程接口. socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. IPv4套接口地址结构通常也称为“网际套接字地址结构”,它 ...

  9. UNIX网络编程——socket概述和字节序、地址转换函数

    一.什么是socket socket可以看成是用户进程与内核网络协议栈的编程接口.socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. socket API是一层抽象的网络 ...

随机推荐

  1. 7.7 C++基本关联式容器

    参考:http://www.weixueyuan.net/view/6404.html 总结: 基本的关联式容器主要有:set.multiset.map和multimap,这四种容器可以分为两组:ma ...

  2. CKEditor编辑器的使用

    <div class="form-group" id="infolink-edier-div"> <label for="" ...

  3. 奇怪问题之@RequestBody问题

    在项目中使用到了@RequestBody注解:该注解的作用是获取Request请求中body中的数据:最近测试项目的时候发现调用该接口的时候直接返回状态400,当将@RequestBody注解去掉以后 ...

  4. iframe 常见问题 解析

    1. jquery在iframe子页面获取父页面元素代码如下: $("#objid",parent.document) 2. jquery在父页面获取iframe子页面的元素代码如 ...

  5. shell日常实战练习——通过监视用户登陆找到入侵者

    #!/usr/bin/bash #用户检测入侵工具 AUTHLOG=/var/log/secure if [[ -n $1 ]];then AUTHLOG=$1 echo "Using Lo ...

  6. 搭建Django项目

    命令行搭建Django项目 1.安装django 在指定解释器环境下安装django 1.11.9 在真实python3环境下: pip3 install django==1.11.9 在虚拟环境下: ...

  7. python-web-django前后端交互

    1.前端请求数据URL由谁来写 在开发中,URL主要是由后台来写好给前端. 若后台在查询数据,需要借助查询条件才能查询到前端需要的数据时,这时后台会要求前端提供相关的查询参数(即URL请求的参数). ...

  8. Java中的13个原子操作类

    java.util.concurrent.atomic包一共提供了13个类.属于4种类型的原子更新方式,分别是原子更新基本类型,原子更新数组,原子更新引用和原子更新属性.Atomic包里的类基本都是使 ...

  9. 训练 の Bessie的体重问题

    Bessie的体重问题 题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉.所以FJ将她置于一个及其严格的节食计划之中.她每天不能吃多过H ...

  10. 【转】Skynet之消息队列 - 消息的存储与分发

    Skynet之消息队列 - 消息的存储与分发 http://www.outsky.org/code/skynet-message-queue.html Sep 8, 2014 按我的理解,消息队列是S ...