Xilinux PetaLinuxビルド環境を準備する

# はじめに

インストール作業した時のメモを残しておきます。

参考にする資料

https://docs.amd.com/r/en-US/ug1144-petalinux-tools-reference-guide/Overview

# Host OSでの準備

https://adaptivesupport.amd.com/s/article/000037095?language=en_US

2024.2_PetaLinux_OS_Package_List.xlsx に記載されているパッケージをインストールする。

Excelのファイルではなく、インストールするコマンドを書いて欲しかった。

```
sudo apt update && sudo apt install -y \
  gcc-13 \
  g++-13 \
  cpp-13 \
  gcc-multilib \
  build-essential \
  automake \
  libstdc++6 \
  patch \
  diffutils \
  debianutils \
  iputils-ping \
  screen \
  xterm \
  openssl \
  libselinux1 \
  libselinux1-dev \
  zlib1g-dev \
  ncurses-dev \
  xz-utils \
  python3 \
  cpio \
  gnupg
```

PetaLinux 2024.2をダウンロードする。

2024/2の場合は、以下のリンク
https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/embedded-design-tools/2024-2.html


Installing the PetaLinux Toolの章に従ってインストーラを実行する。


私は、arm今の所、のみでよいので、オプション設定しました。

```
mkdir -p ~/petalinux/2024.2
cd ~/petalinux/2024.2
../../petalinux-v2024.2-11062026-installer.run --platform "arm"
```

コマンドプロンプトで入力を求めれれるので、それに従いインストールを完了する

.bashrcに以下を追加する。

```
source ~/petalinux/2024.2/settings.sh
```

# /bin/shをbashに変更する

```
masa@Ubuntu24:/bin$ ls -la sh
lrwxrwxrwx 1 root root 4 Mar 31  2024 sh -> dash
masa@Ubuntu24:/bin$ sudo rm sh
masa@Ubuntu24:/bin$ sudo ln -s bash sh
masa@Ubuntu24:/bin$ ls -la sh
lrwxrwxrwx 1 root root 4 Jun  7 05:23 sh -> bash
```

作業中に不足が判明したので、以下のコマンドを実行しておく。
```
cd /lib/x86_64-linux-gnu
sudo ln -s libtinfo.so.6 libtinfo.so.5
```

# EBAZ4205でのPetaLinuxビルド

```
petalinux-create --type project --template zynq --name ebaz4205_simple
petalinux-config --get-hw-description=/home/masa/fpga/arm_ps_uart/design_1_wrapper.xsa
# ドキュメントに uid_map error　のエラー回避のために次のコマンドを実行と記載されていた
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
petalinux-build
```

Device Tree をビルド中にエラーが発生。NANDが有効だが、配線されていない事が原因らしい。
Vivadoで、NANDを無効にしてXSAファイルを再作成する

meta-user/recipes-bsp/fsbl-firmware/fsbl-firmware_2024.2.bbappend を作成
内容は以下
```
# meta-user/recipes-bsp/fsbl-firmware/fsbl-firmware_2024.2.bbappend

EXTRA_OECONF:append = " -processor ps7_cortexa9_0"
```

# SDカードイメージを作成

```
cd /home/masa/petalinux/ebaz4205_simple/ebaz4205_simple/images/linux/
petalinux-package boot   --fsbl zynq_fsbl.elf --u-boot   --fpga system.bit   --force
```

SDカードに、BOOT.BIN, image.ub をコピーする。

U-Bootのプロンプトが表示されたら次のコマンドを入力する

```
setenv bootargs console=ttyPS0,115200 root=/dev/ram0 rw earlycon
setenv bootcmd 'load mmc 0:1 0x3000000 image.ub; bootm 0x3000000'
saveenv
```

Ethernetの回路を入れてなかったので、ネットワークのエラーが発生しましたが、カーネルは起動しました。

```
U-Boot 2024.01 (Oct 24 2024 - 10:42:51 +0000)

CPU:   Zynq 7z010
Silicon: v3.1
DRAM:  ECC disabled 256 MiB
Core:  20 devices, 14 uclasses, devicetree: board
Flash: 0 Bytes
NAND:  0 MiB
MMC:   mmc@e0100000: 0
Loading Environment from FAT... *** Warning - some problems detected reading environment; recovered successfully
OK
In:    serial@e0001000
Out:   serial@e0001000
Err:   serial@e0001000
Net:   Could not get PHY for eth0: addr -19
No ethernet found.

Hit any key to stop autoboot:  0
25243875 bytes read in 1390 ms (17.3 MiB/s)
## Loading kernel from FIT Image at 03000000 ...
   Using 'conf-system-top.dtb' configuration
   Verifying Hash Integrity ... OK
   Trying 'kernel-1' kernel subimage
     Description:  Linux kernel
     Type:         Kernel Image
     Compression:  uncompressed
     Data Start:   0x030000ec
     Data Size:    25223600 Bytes = 24.1 MiB
     Architecture: ARM
     OS:           Linux
     Load Address: 0x00200000
     Entry Point:  0x00200000
     Hash algo:    sha256
     Hash value:   489012462cb608d9e1deaa9980f4572fddb485395c188e818969a5267d9bd31a
   Verifying Hash Integrity ... sha256+ OK
## Loading fdt from FIT Image at 03000000 ...
   Using 'conf-system-top.dtb' configuration
   Verifying Hash Integrity ... OK
   Trying 'fdt-system-top.dtb' fdt subimage
     Description:  Flattened Device Tree blob
     Type:         Flat Device Tree
     Compression:  uncompressed
     Data Start:   0x0480e3a8
     Data Size:    18351 Bytes = 17.9 KiB
     Architecture: ARM
     Hash algo:    sha256
     Hash value:   8268fbd0ad8514e00c06fa4109491bead44d7ccdf8829711da49b55c85c092d0
   Verifying Hash Integrity ... sha256+ OK
   Booting using the fdt blob at 0x480e3a8
Working FDT set to 480e3a8
Could not get PHY for eth0: addr -19
Could not get PHY for eth0: addr -19
   Loading Kernel Image
   Loading Device Tree to 0dabb000, end 0dac27ae ... OK
Working FDT set to dabb000

Starting kernel ...

Booting Linux on physical CPU 0x0
Linux version 6.6.40-xilinx-g2b7f6f70a62a (oe-user@oe-host) (arm-xilinx-linux-gnueabi-gcc (GCC) 13.3.0, GNU ld (GNU Binutils) 2.42.0.20240716) #1 SMP PREEMPT Tue Oct 29 11:52:30 UTC 2024
CPU: ARMv7 Processor [413fc090] revision 0 (ARMv7), cr=18c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
OF: fdt: Machine model: xlnx,zynq-7000
earlycon: cdns0 at MMIO 0xe0001000 (options '115200n8')
printk: bootconsole [cdns0] enabled
Memory policy: Data cache writealloc
cma: Reserved 16 MiB at 0x0f000000 on node -1
Zone ranges:
  Normal   [mem 0x0000000000000000-0x000000000fffffff]
  HighMem  empty
Movable zone start for each node
Early memory node ranges
  node   0: [mem 0x0000000000000000-0x000000000fffffff]
Initmem setup node 0 [mem 0x0000000000000000-0x000000000fffffff]
percpu: Embedded 12 pages/cpu s17940 r8192 d23020 u49152
Kernel command line: console=ttyPS0,115200 root=/dev/ram0 rw earlycon
Dentry cache hash table entries: 32768 (order: 5, 131072 bytes, linear)
Inode-cache hash table entries: 16384 (order: 4, 65536 bytes, linear)
Built 1 zonelists, mobility grouping on.  Total pages: 65024
mem auto-init: stack:all(zero), heap alloc:off, heap free:off
Memory: 197336K/262144K available (7168K kernel code, 251K rwdata, 2128K rodata, 34816K init, 146K bss, 48424K reserved, 16384K cma-reserved, 0K highmem)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=2, Nodes=1
rcu: Preemptible hierarchical RCU implementation.
rcu:    RCU event tracing is enabled.
rcu:    RCU restricting CPUs from NR_CPUS=4 to nr_cpu_ids=2.
rcu: RCU calculated value of scheduler-enlistment delay is 10 jiffies.
rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=2
NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
efuse mapped to (ptrval)
slcr mapped to (ptrval)
GIC physical location is 0xf8f01000
L2C: platform modifies aux control register: 0x72360000 -> 0x72760000
L2C: DT/platform modifies aux control register: 0x72360000 -> 0x72760000
L2C-310 erratum 769419 enabled
L2C-310 enabling early BRESP for Cortex-A9
L2C-310 full line of zeros enabled for Cortex-A9
L2C-310 ID prefetch enabled, offset 1 lines
L2C-310 dynamic clock gating enabled, standby mode enabled
L2C-310 cache controller enabled, 8 ways, 512 kB
L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x76760001
rcu: srcu_init: Setting srcu_struct sizes based on contention.
zynq_clock_init: clkc starts at (ptrval)
Zynq clock init
sched_clock: 64 bits at 167MHz, resolution 6ns, wraps every 4398046511103ns
clocksource: arm_global_timer: mask: 0xffffffffffffffff max_cycles: 0x26703d7dd8, max_idle_ns: 440795208065 ns
Switching to timer-based delay loop, resolution 6ns
Console: colour dummy device 80x30
Calibrating delay loop (skipped), value calculated using timer frequency.. 333.33 BogoMIPS (lpj=1666666)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
Setting up static identity map for 0x100000 - 0x100060
rcu: Hierarchical SRCU implementation.
rcu:    Max phase no-delay instances is 1000.
smp: Bringing up secondary CPUs ...
CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
CPU1: Spectre v2: using BPIALL workaround
smp: Brought up 1 node, 2 CPUs
SMP: Total of 2 processors activated (666.66 BogoMIPS).
CPU: All CPU(s) started in SVC mode.
devtmpfs: initialized
VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 4
clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
futex hash table entries: 512 (order: 3, 32768 bytes, linear)
pinctrl core: initialized pinctrl subsystem
NET: Registered PF_NETLINK/PF_ROUTE protocol family
DMA: preallocated 256 KiB pool for atomic coherent allocations
thermal_sys: Registered thermal governor 'step_wise'
cpuidle: using governor menu
platform axi: Fixed dependency cycle(s) with /axi/interrupt-controller@f8f01000
platform replicator: Fixed dependency cycle(s) with /axi/etb@f8801000
amba f8801000.etb: Fixed dependency cycle(s) with /replicator
platform replicator: Fixed dependency cycle(s) with /axi/tpiu@f8803000
amba f8803000.tpiu: Fixed dependency cycle(s) with /replicator
platform replicator: Fixed dependency cycle(s) with /axi/funnel@f8804000
amba f8804000.funnel: Fixed dependency cycle(s) with /axi/ptm@f889d000
amba f8804000.funnel: Fixed dependency cycle(s) with /axi/ptm@f889c000
amba f8804000.funnel: Fixed dependency cycle(s) with /replicator
amba f8804000.funnel: Fixed dependency cycle(s) with /axi/ptm@f889c000
amba f889c000.ptm: Fixed dependency cycle(s) with /axi/funnel@f8804000
amba f8804000.funnel: Fixed dependency cycle(s) with /axi/ptm@f889d000
amba f889d000.ptm: Fixed dependency cycle(s) with /axi/funnel@f8804000
hw-breakpoint: found 5 (+1 reserved) breakpoint and 1 watchpoint registers.
hw-breakpoint: maximum watchpoint size is 4 bytes.
e0000000.serial: ttyPS1 at MMIO 0xe0000000 (irq = 26, base_baud = 6249999) is a xuartps
e0001000.serial: ttyPS0 at MMIO 0xe0001000 (irq = 27, base_baud = 6249999) is a xuartps
printk: console [ttyPS0] enabled
printk: console [ttyPS0] enabled
printk: bootconsole [cdns0] disabled
printk: bootconsole [cdns0] disabled
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
mc: Linux media interface: v0.10
videodev: Linux video capture interface: v2.00
pps_core: LinuxPPS API ver. 1 registered
pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
PTP clock support registered
EDAC MC: Ver: 3.0.0
FPGA manager framework
Advanced Linux Sound Architecture Driver Initialized.
vgaarb: loaded
clocksource: Switched to clocksource arm_global_timer
NET: Registered PF_INET protocol family
IP idents hash table entries: 4096 (order: 3, 32768 bytes, linear)
tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 4096 bytes, linear)
Table-perturb hash table entries: 65536 (order: 6, 262144 bytes, linear)
TCP established hash table entries: 2048 (order: 1, 8192 bytes, linear)
TCP bind hash table entries: 2048 (order: 3, 32768 bytes, linear)
TCP: Hash tables configured (established 2048 bind 2048)
UDP hash table entries: 256 (order: 1, 8192 bytes, linear)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes, linear)
NET: Registered PF_UNIX/PF_LOCAL protocol family
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp-with-tls transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
PCI: CLS 0 bytes, default 64
armv7-pmu f8891000.pmu: hw perfevents: no interrupt-affinity property, guessing.
hw perfevents: enabled with armv7_cortex_a9 PMU driver, 7 counters available
workingset: timestamp_bits=30 max_order=16 bucket_order=0
jffs2: version 2.2. (NAND) (SUMMARY)  c 2001-2006 Red Hat, Inc.
io scheduler mq-deadline registered
io scheduler kyber registered
io scheduler bfq registered
zynq-pinctrl 700.pinctrl: zynq pinctrl initialized
dma-pl330 f8003000.dma-controller: Loaded driver for PL330 DMAC-241330
dma-pl330 f8003000.dma-controller:      DBUFF-128x8bytes Num_Chans-8 Num_Peri-4 Num_Events-16
brd: module loaded
loop: module loaded
CAN device driver interface
macb e000b000.ethernet eth0: Cadence GEM rev 0x00020118 at 0xe000b000 irq 40 (00:0a:35:00:1e:53)
e1000e: Intel(R) PRO/1000 Network Driver
e1000e: Copyright(c) 1999 - 2015 Intel Corporation.
usbcore: registered new interface driver usb-storage
i2c_dev: i2c /dev entries driver
cdns-i2c e0004000.i2c: can't get pinctrl, bus recovery not supported
cdns-i2c e0004000.i2c: 400 kHz mmio e0004000 irq 41
cdns-wdt f8005000.watchdog: Xilinx Watchdog Timer with timeout 10s
EDAC MC: ECC not enabled
Xilinx Zynq CpuIdle Driver started
sdhci: Secure Digital Host Controller Interface driver
sdhci: Copyright(c) Pierre Ossman
sdhci-pltfm: SDHCI platform and OF driver helper
ledtrig-cpu: registered to indicate activity on CPUs
clocksource: ttc_clocksource: mask: 0xffff max_cycles: 0xffff, max_idle_ns: 537538477 ns
timer #0 at (ptrval), irq=44
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
fpga_manager fpga0: Xilinx Zynq FPGA Manager registered
NET: Registered PF_INET6 protocol family
mmc0: SDHCI controller on e0100000.mmc [e0100000.mmc] using ADMA
Segment Routing with IPv6
In-situ OAM (IOAM) with IPv6
sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
NET: Registered PF_PACKET protocol family
can: controller area network core
NET: Registered PF_CAN protocol family
can: raw protocol
can: broadcast manager protocol
can: netlink gateway - max_hops=1
Registering SWP/SWPB emulation handler
of-fpga-region fpga-region: FPGA Region probed
of_cfs_init
of_cfs_init: OK
clk: Disabling unused clocks
ALSA device list:
  No soundcards found.
mmc0: new high speed SDHC card at address 1234
mmcblk0: mmc0:1234 SA32G 28.9 GiB
 mmcblk0: p1 p2
Freeing unused kernel image (initmem) memory: 34816K
Run /init as init process
INIT: version 3.04 booting
Starting udev
udevd[76]: starting version 3.2.14
random: crng init done
udevd[77]: starting eudev-3.2.14
macb e000b000.ethernet enx000a35001e53: renamed from eth0
ext3: Unknown parameter 'umask'
ext2: Unknown parameter 'umask'
ext4: Unknown parameter 'umask'
FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
EXT4-fs (mmcblk0p2): recovery complete
EXT4-fs (mmcblk0p2): mounted filesystem 33d8b6cd-b432-4bf7-8627-dc255d78d93b r/w with ordered data mode. Quota mode: disabled.
hwclock: can't open '/dev/misc/rtc': No such file or directory
Fri Mar  9 12:34:56 UTC 2018
hwclock: can't open '/dev/misc/rtc': No such file or directory
hwclock: can't open '/dev/misc/rtc': No such file or directory
INIT: Entering runlevel: 5
Configuring network interfaces... udhcpc: SIOCGIFINDEX: No such device
ifup: failed to bring up eth0
udhcpc: started, v1.36.1
macb e000b000.ethernet enx000a35001e53: validation of  with support 00,00000000,00000000,00006000 and advertisement 00,00000000,00000000,00000000 failed: -EINVAL
macb e000b000.ethernet enx000a35001e53: Could not attach PHY (-22)
RTNETLINK answers: Invalid argument
udhcpc: broadcasting discover
udhcpc: sendto: Network is down
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket
```