Pages

2017-01-19

Raspberry Pi - sdb2 super block corruption


* Superblock : bad magic number (dump file link)

$ sudo dd if=/dev/sdb2 bs=1024 count=2 | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000380  b0 c9 0e 00 80 19 3b 00  98 6d 02 00 17 26 22 00  |......;..m...&".|
00000390  fc 5d 0b 00 00 00 00 00  02 00 00 00 02 00 00 00  |.]..............|
000003a0  00 80 00 00 00 80 00 00  d0 1f 00 00 3e d2 2f 58  |............>./X|
000003b0  03 00 00 00 03 00 ff ff  53 ef 03 00 01 00 00 00  |........S.......|
000003c0  2d c4 2f 58 b0 c9 0e 00  80 19 3b 00 98 6d 02 00  |-./X......;..m..|
000003d0  53 25 22 00 fc 5d 0b 00  00 00 00 00 02 00 00 00  |S%"..]..........|
000003e0  02 00 00 00 00 80 00 00  00 80 00 00 d0 1f 00 00  |................|
000003f0  bf d2 2f 58 03 00 00 00  05 00 ff ff 53 ef 03 00  |../X........S...|
00000400  01 00 00 00 3d d2 2f 58  00 00 00 00 00 00 00 00  |....=./X........|
00000410  01 00 00 00 00 00 00 00  0b 00 00 00 00 01 00 00  |................|
00000420  3c 00 00 00 42 02 00 00  73 00 00 00 ad 62 03 a1  |<...B...s....b..|
00000430  ec 50 4f 44 a1 c0 e6 c3  dd 4c 92 02 00 00 00 00  |.POD.....L......|
00000440  00 00 00 00 00 00 00 00  00 00 00 00 2f 00 65 64  |............/.ed|
00000450  69 61 2f 63 6d 6a 6f 2f  61 64 36 32 30 33 61 31  |ia/cmjo/ad6203a1|
00000460  2d 65 63 35 30 2d 34 66  34 34 2d 61 31 63 30 2d  |-ec50-4f44-a1c0-|
00000470  65 36 63 33 64 64 34 63  39 32 30 32 00 00 00 00  |e6c3dd4c9202....|
00000480  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000490  00 00 e6 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000004a0  00 00 00 00 08 00 00 00  00 00 00 00 00 00 00 00  |................|
000004b0  58 e2 0c f7 9d 8c 4c 05  8d eb a6 52 51 90 7e 3c  |X.....L....RQ.~<|
000004c0  01 01 00 00 0c 00 00 00  00 00 00 00 6e c8 50 56  |............n.PV|
000004d0  0a f3 01 00 04 00 00 00  00 00 00 00 00 00 00 00  |................|
000004e0  00 40 00 00 00 80 06 00  00 00 00 00 00 00 00 00  |.@..............|
000004f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000510  00 00 00 04 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000520  1c 00 1c 00 02 00 00 00  f1 7f 00 00 00 00 00 00  |................|
00000530  00 00 00 00 00 00 00 00  04 00 00 00 ba 7b cc 02  |.............{..|
00000540  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000550  00 00 00 00 00 00 00 00  01 00 00 00 11 00 00 00  |................|
00000560  6b 54 00 00 00 00 00 00  00 00 00 00 65 78 74 34  |kT..........ext4|
00000570  5f 69 67 65 74 00 00 00  00 00 00 00 00 00 00 00  |_iget...........|
00000580  00 00 00 00 00 00 00 00  00 00 00 00 a6 10 00 00  |................|
00000590  11 00 00 00 6b 54 00 00  a6 10 00 00 00 00 00 00  |....kT..........|
000005a0  00 00 00 00 65 78 74 34  5f 69 67 65 74 00 00 00  |....ext4_iget...|
000005b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|




* Superblock : after fsck (dump file link)

$ sudo fsck /dev/sdb2
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks...
/dev/sdb2: recovering journal
fsck.ext2: unable to set superblock flags on /dev/sdb2


/dev/sdb2: ***** FILE SYSTEM WAS MODIFIED *****

/dev/sdb2: ********** WARNING: Filesystem still has errors **********
 

$ sudo fsck /dev/sdb2
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
Setting free inodes count to 744873 (was 744872)
/dev/sdb2: clean, 224263/969136 files, 1620552/3873152 blocks


$ sudo dd if=/dev/sdb2 bs=1024 count=2 | hexdump -C
2+0 records in
2+0 records out
2048 bytes (2.0 kB, 2.0 KiB) copied, 0.00621587 s, 329 kB/s
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000380  b0 c9 0e 00 80 19 3b 00  98 6d 02 00 17 26 22 00  |......;..m...&".|
00000390  fc 5d 0b 00 00 00 00 00  02 00 00 00 02 00 00 00  |.]..............|
000003a0  00 80 00 00 00 80 00 00  d0 1f 00 00 3e d2 2f 58  |............>./X|
000003b0  03 00 00 00 03 00 ff ff  53 ef 03 00 01 00 00 00  |........S.......|
000003c0  2d c4 2f 58 b0 c9 0e 00  80 19 3b 00 98 6d 02 00  |-./X......;..m..|
000003d0  53 25 22 00 fc 5d 0b 00  00 00 00 00 02 00 00 00  |S%"..]..........|
000003e0  02 00 00 00 00 80 00 00  00 80 00 00 d0 1f 00 00  |................|
000003f0  bf d2 2f 58 03 00 00 00  05 00 ff ff 53 ef 03 00  |../X........S...|
00000400  b0 c9 0e 00 80 19 3b 00  98 6d 02 00 38 5f 22 00  |......;..m..8_".|
00000410  a9 5d 0b 00 00 00 00 00  02 00 00 00 02 00 00 00  |.]..............|
00000420  00 80 00 00 00 80 00 00  d0 1f 00 00 1e 6c 20 57  |.............l W|
00000430  bc 48 80 58 03 00 ff ff  53 ef 01 00 01 00 00 00  |.H.X....S.......|
00000440  67 75 20 57 00 00 00 00  00 00 00 00 01 00 00 00  |gu W............|
00000450  00 00 00 00 0b 00 00 00  00 01 00 00 3c 00 00 00  |............<...|
00000460  42 02 00 00 73 00 00 00  ad 62 03 a1 ec 50 4f 44  |B...s....b...POD|
00000470  a1 c0 e6 c3 dd 4c 92 02  00 00 00 00 00 00 00 00  |.....L..........|
00000480  00 00 00 00 00 00 00 00  2f 00 6e 74 2f 73 64 63  |......../.nt/sdc|
00000490  61 72 64 00 64 65 76 2f  70 69 2d 67 65 6e 2f 65  |ard.dev/pi-gen/e|
000004a0  78 70 6f 72 74 2d 6a 65  73 73 69 65 2f 72 6f 6f  |xport-jessie/roo|
000004b0  74 66 73 00 00 00 00 00  00 00 00 00 00 00 00 00  |tfs.............|
000004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 e6 00  |................|
000004d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000004e0  08 00 00 00 00 00 00 00  00 00 00 00 58 e2 0c f7  |............X...|
000004f0  9d 8c 4c 05 8d eb a6 52  51 90 7e 3c 01 01 00 00  |..L....RQ.~<....|
00000500  0c 00 00 00 00 00 00 00  6e c8 50 56 0a f3 01 00  |........n.PV....|
00000510  04 00 00 00 00 00 00 00  00 00 00 00 00 40 00 00  |.............@..|
00000520  00 80 06 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000530  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000540  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 04  |................|
00000550  00 00 00 00 00 00 00 00  00 00 00 00 1c 00 1c 00  |................|
00000560  02 00 00 00 f1 7f 00 00  00 00 00 00 00 00 00 00  |................|
00000570  00 00 00 00 04 00 00 00  06 57 cc 02 00 00 00 00  |.........W......|
00000580  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000800



* Superblock : after RPi more fixed (only dump file link)


* Analysis
 - According to the Ext4 disk layout,
   first 1024 byte (if the disk size is 16GB) must be 0 padded.
 - But, the real data was not. (0x400 = 1024)
 - And the magic signature, 0x0438 and 0x0439 is not 0xEF53 before fixing.


* Question
 - Why the first 1024 byte is not 0 padded?
 - What causes this corruption?


2017-01-18

Raspberry Pi MBR - Partition Expansion



* SD Card is SanDisk 16GB


* Before Expansion (linux partition is about 9GB)

  $ hexdump -C t20r18-mbr-20161031.img
00000000  fa b8 00 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |................|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  1f da 06 00 00 00 00 82  |................|
000001c0  03 00 0c 28 20 08 00 20  00 00 00 e0 01 00 00 00  |...( .. ........|
000001d0  01 00 83 fe ff ff 00 00  02 00 00 00 fa
00 00 00  |................|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400




* After Expansion (linux partition is about 15GB)

  $ hexdump -C sd_fixed.img
00000000  fa b8 00 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |................|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  1f da 06 00 00 00 00 82  |................|
000001c0  03 00 0c 28 20 08 00 20  00 00 00 e0 01 00 00 00  |...( .. ........|
000001d0  01 00 83 03 d0 2f 00 00  02 00 00 cc d8 01
00 00  |...../..........|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400



* Analysis
  - 0x01ce | Status         | Value 0x00   => Valid (0x00 or 0x80)
  - 0x01cf | Head           | Value 0x00
  - 0x01d0 | Sector         | Value 0x01
  - 0x01d1 | Cylinder       | Value 0x00
  - 0x01d2 | Partition Type | Value 0x83   => Linux
    =================== below values are different =====================
  - 0x01d3 | Head           | Value 0x03
  - 0x01d4 | Sector         | Value 0xd0
  - 0x01d5 | Cylinder       | Value 0x2f
  - 0x01d6 ~ 0x01d9 : LBA of first absolute sector in the partition
  - 0x01da ~ 0x01dd : Number of sectors in partition


2013-12-30

xbionic - ntdll.dll (Windows Native APIs)


1. [ntdll.dll] Header and Code from the scratch

    For implementing the system-call of Linux, Windows Native APIs will be used.
    And the API-set will be re-written from the scrach.

    [WinDDK] has the useful package to start, but that is too complicate.

    So, the structures and functions are extracted one by one whenever it is used.


2. Current Codes

    The porting process is on the stable stage, so the file system is almost fixed.
    See the below files :

    - Main Headers
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/ntstatus.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/nttypes.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/ntdll.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/ntsock.h

    - Supporting Headers
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/poppack.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/pshpack1.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/pshpack2.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/pshpack4.h
       https://github.com/webos21/xbionic/blob/master/lib/mingw32/include/pshpack8.h

    - Implementing Source
       https://github.com/webos21/xbionic/blob/master/xbionic/libc/arch-win/_nt_imp/ntdll.c
       https://github.com/webos21/xbionic/blob/master/xbionic/libc/arch-win/_nt_imp/ntsock.c

xbionic - bionic libc for windows


1. What is [xbionic]


1-1. Bionic Libc

    Android has the own standard c library, Bionic Libc.
    It is just for the Linux OS, more precisely the Linux Kernel.

    The implementation is very dependent on the System Call of Linux.
    Because it stands for providing the light-weight standard c library, so it is natural.

    Android version 4.4 (codename KitKat) has began to support the 64-bit CPU.
    So, the Bionic Libc is evolving to support it.


1-2. Cygwin & MinGW for Developing on Windows

    Cygwin(or MinGW) is the best choice to develop on Windows OS.

    But, there is some missing features. For example, [the Group Source on Multicast],
    [Getting the list of Ethernet Interfaces], [Socket Options], [Signal] and so on.

    Inspite of these missing features, it is quite useful.


1-3. Bionic Libc for Windows
    For overcome the missing features of Cygwin(or MinGW) and getting more flexible
    standard c library, it is started the porting Bionic Libc on Windows 7.

    [xbionic] is that project.  And, it is also the begining of Android Platform for Windows.
    That means running the Android Apps directly on Windows without QEMU.


2. the Way to port the Bionic Libc on Windows

2-1. only use the [ntdll.dll]

    [ntdll.dll] has the Windows Native API.  It is not documented by Microsoft.
    But, [WinDDK] has small piece of reference code and documents,
    and the undocumented project of NTinternal - http://undocumented.ntinternals.net/ -
    has the good API documents.  Also, the ReactOS Project has the good usage.

    On the source tree of Bionic Libc, libc/arch-x86 is the base directory
    to fork the [arch-win].
    And the implementation of linux system-call is re-written by Windows Native APIs.


2-2. Building System

    Same as any other android libraries, bionic libc has android building system
    - [Android.mk].

    But, I need the independent building system, so the Make and ANT is used
    for compiling and linking the project.


2-3. VCS - GitHub

    The project is started on GitHub.  https://github.com/webos21/xbionic.git
    Anyone can participate and help.



2013-06-22

Designing 64-bit compatible library


[Compiler : 64-bit data model]

First of all, you must check the 64-bit data model of your compiler.

In case of 64-bit supporting compiler,
there is the 64-bit data model(about the length of the c primitive types).
When you search about the 64-bit programming,
you can see the many articles such as LLP64 / LP64 / ILP64(Integer, Long, Pointer).

  - LLP64 : only pointer type is 64-bit. (MSVC)
  - LP64 : long & pointer types are 64-bit (GCC)
  - ILP64 : int & long & pointer types are 64-bit (just few case)

  * You can find the detail information on http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models.

I learned that the INTEGER type follows the size of CPU generic register,
in the time of changing from 16-bit to 32-bit.
But, in this time that information is wrong.

I guess that the gurus decide to avoid the huge modification of legacy codes
by changing the size of INTEGER. 
(Maybe, the Microsoft decides to retain the length of LONG type because of this reason.)


[Important types : size_t / ssize_t / offset_t]

The 64-bit system can handle the large amount of memory and storage
with these types.

You can see that these types are defined as LONG 
in the header files of Linux. (LP system)

Of cause the 32-bit system must be able to handle the large storage,
so there is "off64_t" when you use the "_LAGEFILE64_SOURCE" or "_FILE_OFFSET_BITS=64" in compile time.

We have to understand of these type for designing the compatible library both 32-bit and 64-bit.

The system calls those we frequently use "read", "memcpy", "strncpy"
are defined as below:

    ssize_t read(int fd, void *buf, size_t count);
    void *memcpy(void *dest, const void *src, size_t n);
    char *strncpy(char *dest, const char *src, size_t n);

MSVC also has similar types such as "SIZE_T", "SSIZE_T",
and the size of those is changed from 32-bit system to 64-bit.


[Use the : intptr_t / uintptr_t]

In case of the "pthread" API,
"pthread_t" type to be used as the thread handle is LONG.
It looks like a decimal value, but the real value is the pointer address.

In windows programming, the "WPARAM" and "LPARAM" to be used in event loop
are cast to the pointer value frequently.

It is vary useful to cast between INTEGER and POINTER
for hiding the structure and eliminating the dependency.

But, in the LP data model, the size of INTEGER is still 32-bit,
and the size of POINTER is 64-bit.
This difference can cause the bugs and system fault.

For solving this problem, there is "intptr_t" type in the C99 specification.

The standard header file "stdint.h" is
introduced for converting a pointer to integer exactly.

So, you can be free just using the "intptr_t" in that case.


[Examples]

You can see the custom type definition
in "glib" or "APR(Apache Portable Runtime)".
It is used for overcoming the differences of systems or compilers.

So, those custom definitions are like as below :


/************************************************
 * Primitive Types Definition
 * ----------------------------------------------
 * References the inttypes.h
 * Both Win32 and Linux are equal.
 ************************************************/

typedef void               xvoid;    ///< define the type : void to xvoid

#ifdef __cplusplus
typedef bool               xbool;    ///< define the type : bool to xbool
#else // !__cplusplus
typedef unsigned char      xbool;    ///< define the type : unsigned char to xbool
#endif // __cplusplus

typedef char               xchar;    ///< define the type : char to xchar

typedef char               xint8;    ///< define the type : char to xint8
typedef unsigned char      xuint8;   ///< define the type : unsigned char to xuint8

typedef short              xint16;   ///< define the type : short to xint16
typedef unsigned short     xuint16;  ///< define the type : unsigned short to xuint16

typedef int                xint32;   ///< define the type : int to xint32
typedef unsigned int       xuint32;  ///< define the type : unsigned int to xuint32

typedef long long          xint64;   ///< define the type : long long to xint64
typedef unsigned long long xuint64;  ///< define the type : unsigned long long to xuint64

typedef float              xfloat32; ///< define the type : float to xfloat32
typedef double             xfloat64; ///< define the type : double to xfloat64




/************************************************
 * Pointer Type Definition
 * ----------------------------------------------
 * for both 32-bit and 64-bit (LP Architecture)
 ************************************************/

#ifdef _WIN32
#    ifdef _WIN64
typedef __int64            xintptr;  ///< define the long to integer-pointer
typedef unsigned __int64   xuintptr; ///< define the unsigned long to unsigned integer-pointer
#    else // !_WIN64
typedef long               xintptr;  ///< define the long to integer-pointer
typedef unsigned long      xuintptr; ///< define the unsigned long to unsigned integer-pointer
#    endif // _WIN64
#else // !_WIN32
typedef long               xintptr;  ///< define the long to integer-pointer
typedef unsigned long      xuintptr; ///< define the unsigned long to unsigned integer-pointer
#endif // _WIN32



/************************************************
 * Size and Offset Type Definition
 * ----------------------------------------------
 * for both 32-bit and 64-bit (LP Architecture)
 ************************************************/

#if defined(__x86_64) || defined(__amd64)
typedef long unsigned int  xsize;
typedef long               xoff64;  ///< define the off64 to long (64bit)
#else // !(define(__x86_64) || define(__amd64))
typedef unsigned int       xsize;
typedef long long          xoff64;  ///< define the off64 to long long (64bit)
#endif // define(__x86_64) || define(__amd64)



And, using the custom types, you can define the interface like below :


/**
 * Copies len bytes from memory area src to memory area dest.
 * The memory areas should not overlap.
 *
 * @param dest The destination pointer to be copied
 * @param src The source pointer to be copied
 * @param len The length to be copied
 * @return Same pointer as the destination pointer
 */
xvoid  *xi_mem_copy(xvoid *dest, const xvoid *src, xsize len);