Hardwarebug : Everything is broken

http://hardwarebug.org/

Les articles publiés sur le site

  • Countryside

    17 août 2010, par MansPhotos










  • Countryside

    17 août 2010, par MansPhotos
  • ARM inline asm secrets

    6 juillet 2010, par MansARM, Compilers

    Although I generally recommend against using GCC inline assembly, preferring instead pure assembly code in separate files, there are occasions where inline is the appropriate solution. Should one, at a time like this, turn to the GCC documentation for guidance, one must be prepared for a degree of disappointment. As it happens, much of the inline asm syntax is left entirely undocumented. This article attempts to fill in some of the blanks for the ARM target.

    Constraints

    Each operand of an inline asm block is described by a constraint string encoding the valid representations of the operand in the generated assembly. For example the “r” code denotes a general-purpose register. In addition to the standard constraints, ARM allows a number of special codes, only some of which are documented. The full list, including a brief description, is available in the constraints.md file in the GCC source tree. The following table is an extract from this file consisting of the codes which are meaningful in an inline asm block (a few are only useful in the machine description itself).

    f Legacy FPA registers f0-f7.
    t The VFP registers s0-s31.
    v The Cirrus Maverick co-processor registers.
    w The VFP registers d0-d15, or d0-d31 for VFPv3.
    x The VFP registers d0-d7.
    y The Intel iWMMX co-processor registers.
    z The Intel iWMMX GR registers.
    l In Thumb state the core registers r0-r7.
    h In Thumb state the core registers r8-r15.
    j A constant suitable for a MOVW instruction. (ARM/Thumb-2)
    b Thumb only. The union of the low registers and the stack register.
    I In ARM/Thumb-2 state a constant that can be used as an immediate value in a Data Processing instruction. In Thumb-1 state a constant in the range 0 to 255.
    J In ARM/Thumb-2 state a constant in the range -4095 to 4095. In Thumb-1 state a constant in the range -255 to -1.
    K In ARM/Thumb-2 state a constant that satisfies the I constraint if inverted. In Thumb-1 state a constant that satisfies the I constraint multiplied by any power of 2.
    L In ARM/Thumb-2 state a constant that satisfies the I constraint if negated. In Thumb-1 state a constant in the range -7 to 7.
    M In Thumb-1 state a constant that is a multiple of 4 in the range 0 to 1020.
    N Thumb-1 state a constant in the range 0 to 31.
    O In Thumb-1 state a constant that is a multiple of 4 in the range -508 to 508.
    Pa In Thumb-1 state a constant in the range -510 to +510
    Pb In Thumb-1 state a constant in the range -262 to +262
    Ps In Thumb-2 state a constant in the range -255 to +255
    Pt In Thumb-2 state a constant in the range -7 to +7
    G In ARM/Thumb-2 state a valid FPA immediate constant.
    H In ARM/Thumb-2 state a valid FPA immediate constant when negated.
    Da In ARM/Thumb-2 state a const_int, const_double or const_vector that can be generated with two Data Processing insns.
    Db In ARM/Thumb-2 state a const_int, const_double or const_vector that can be generated with three Data Processing insns.
    Dc In ARM/Thumb-2 state a const_int, const_double or const_vector that can be generated with four Data Processing insns. This pattern is disabled if optimizing for space or when we have load-delay slots to fill.
    Dn In ARM/Thumb-2 state a const_vector which can be loaded with a Neon vmov immediate instruction.
    Dl In ARM/Thumb-2 state a const_vector which can be used with a Neon vorr or vbic instruction.
    DL In ARM/Thumb-2 state a const_vector which can be used with a Neon vorn or vand instruction.
    Dv In ARM/Thumb-2 state a const_double which can be used with a VFP fconsts instruction.
    Dy In ARM/Thumb-2 state a const_double which can be used with a VFP fconstd instruction.
    Ut In ARM/Thumb-2 state an address valid for loading/storing opaque structure types wider than TImode.
    Uv In ARM/Thumb-2 state a valid VFP load/store address.
    Uy In ARM/Thumb-2 state a valid iWMMX load/store address.
    Un In ARM/Thumb-2 state a valid address for Neon doubleword vector load/store instructions.
    Um In ARM/Thumb-2 state a valid address for Neon element and structure load/store instructions.
    Us In ARM/Thumb-2 state a valid address for non-offset loads/stores of quad-word values in four ARM registers.
    Uq In ARM state an address valid in ldrsb instructions.
    Q In ARM/Thumb-2 state an address that is a single base register.

    Operand codes

    Within the text of an inline asm block, operands are referenced as %0, %1 etc. Register operands are printed as rN, memory operands as [rN, #offset], and so forth. In some situations, for example with operands occupying multiple registers, more detailed control of the output may be required, and once again, an undocumented feature comes to our rescue.

    Special code letters inserted between the % and the operand number alter the output from the default for each type of operand. The table below lists the more useful ones.

    c An integer or symbol address without a preceding # sign
    B Bitwise inverse of integer or symbol without a preceding #
    L The low 16 bits of an immediate constant
    m The base register of a memory operand
    M A register range suitable for LDM/STM
    H The highest-numbered register of a pair
    Q The least significant register of a pair
    R The most significant register of a pair
    P A double-precision VFP register
    p The high single-precision register of a VFP double-precision register
    q A NEON quad register
    e The low doubleword register of a NEON quad register
    f The high doubleword register of a NEON quad register
    h A range of VFP/NEON registers suitable for VLD1/VST1
    A A memory operand for a VLD1/VST1 instruction
    y S register as indexed D register, e.g. s5 becomes d2[1]
  • ARM inline asm secrets

    6 juillet 2010, par MansARM, Compilers
    Although I generally recommend against using GCC inline assembly, preferring instead pure assembly code in separate files, there are occasions where inline is the appropriate solution. Should one, at a time like this, turn to the GCC documentation for guidance, one must be prepared for a degree of disappointment. As … Continue reading
  • Hacking the Popcorn Hour C-200

    3 mai 2010, par MansHardware, MIPS

    Update: A new firmware version has been released since the publication of this article. I do not know if the procedure described below will work with the new version.

    The Popcorn Hour C-200 is a Linux-based media player with impressive specifications. At its heart is a Sigma Designs SMP8643 system on chip with a 667MHz MIPS 74Kf as main CPU, several co-processors, and 512MB of DRAM attached. Gigabit Ethernet, SATA, and USB provide connectivity with the world around it. With a modest $299 on the price tag, the temptation to repurpose the unit as a low-power server or cheap development board is hard to resist. This article shows how such a conversion can be achieved.

    Kernel

    The PCH runs a patched Linux 2.6.22.19 kernel. A source tarball is available from the manufacturer. This contains the sources with Sigma support patches, Con Kolivas’ patch set (scheduler tweaks), and assorted unrelated changes. Properly split patches are unfortunately not available. I have created a reduced patch against vanilla 2.6.22.19 with only Sigma-specific changes, available here.

    The installed kernel has a number of features disabled, notably PTY support and oprofile. We will use kexec to load a more friendly one.

    As might be expected, the PCH kernel does not have kexec support enabled. It does however, by virtue of using closed-source components, support module loading. This lets us turn kexec into a module and load it. A patch for this is available here. To build the module, apply the patch to the PCH sources and build using this configuration. This will produce two modules, kexec.ko and mips_kexec.ko. No other products of this build will be needed.

    The replacement kernel can be built from the PCH sources or, if one prefers, from vanilla 2.6.22.19 with the Sigma-only patch. For the latter case, this config provides a minimal starting point suitable for NFS-root.

    When configuring the kernel, make sure CONFIG_TANGOX_IGNORE_CMDLINE is enabled. Otherwise the command line will be overridden by a useless one stored in flash. A good command line can be set with CONFIG_CMDLINE (under “Kernel hacking” in menuconfig) or passed from kexec.

    Taking control

    In order to load our kexec module, we must first gain root privileges on the PCH, and here a few features of the system are working to our advantage:

    1. The PCH allows mounting any NFS export to access media files stored there.
    2. There is an HTTP server running. As root.
    3. This HTTP server can be readily instructed to fetch files from an NFS mount.
    4. Files with a name ending in .cgi are executed. As root.

    All we need do to profit from this is place the kexec modules, the kexec userspace tools, and a simple script on an NFS export. Once this is done, and the mount point configured on the PCH, a simple HTTP request will send the old kernel screaming to /dev/null, our shiny new kernel taking its place.

    The rootfs

    A kernel is mostly useless without a root filesystem containing tools and applications. A number of tools for cross-compiling a full system exist, each with its strengths and weaknesses. The only thing to look out for is the version of kernel headers used (usually a linux-headers package). As we will be running an old kernel, chances are the default version is too recent. Other than this, everything should be by the book.

    Assembling the parts

    Having gathered all the pieces, it is now time to assemble the hack. The following steps are suitable for an NFS-root system. Adaptation to a disk-based system is left as an exercise.

    1. Build a rootfs for MIPS 74Kf little endian. Make sure kernel headers used are no more recent than 2.6.22.x. Include a recent version of the kexec userspace tools.
    2. Fetch and unpack the PCH kernel sources.
    3. Apply the modular kexec patch.
    4. Using this config, build the modules and install them as usual to the rootfs. The version string must be 2.6.22.19-19-4.
    5. From either the same kernel sources or plain 2.6.22.19 with Sigma patches, build a vmlinux and (optionally) modules using this config. Modify the compiled-in command line to point to the correct rootfs. Set the version string to something other than in the previous step.
    6. Copy vmlinux to any directory in the rootfs.
    7. Copy kexec.sh and kexec.cgi to the same directory as vmlinux.
    8. Export the rootfs over NFS with full read/write permissions for the PCH.
    9. Power on the PCH, and update to latest firmware.
    10. Configure an NFS mount of the rootfs.
    11. Navigate to the rootfs in the PCH UI. A directory listing of bin, dev, etc. should be displayed.
    12. On the host system, run the kexec.sh script with the target hostname or IP address as argument.
    13. If all goes well, the new kernel will boot and mount the rootfs.

    Serial console

    A serial console is indispensable for solving boot problems. The PCH board has two UART connectors. We will use the one labeled UART0. The pinout is as follows (not standard PC pinout).

            +-----------+
           2| * * * * * |10
           1| * * * * * |9
            \-----------+
              J7 UART0
        /---------------------/ board edge
    
    Pin Function
    1 +5V
    5 Rx
    6 Tx
    10 GND

    The signals are 3.3V so a converter, e.g. MAX202, is required for connecting this to a PC serial port. The default port settings are 115200 bps 8n1.