From c94e995c208baa249f4f076780d64b5b4e9d074f Mon Sep 17 00:00:00 2001
From: lharnaldi <lharnaldi@gmail.com>
Date: Wed, 20 Jul 2022 16:00:58 -0300
Subject: [PATCH] Agrgeo los scripts para generar las imagenes de linux

---
 projects/led_blinker/app/index.html   |  24 +++
 projects/led_blinker/app/start.sh     |   7 +
 projects/led_blinker/block_design.tcl |  55 ++++++
 scripts/bitstream.tcl                 |  17 ++
 scripts/core.tcl                      |  48 +++++
 scripts/debian.sh                     | 267 ++++++++++++++++++++++++++
 scripts/devicetree.tcl                |  27 +++
 scripts/fsbl.tcl                      |  20 ++
 scripts/hwdef.tcl                     |   8 +
 scripts/image.sh                      |  19 ++
 scripts/project.tcl                   | 107 +++++++++++
 scripts/ubuntu.sh                     | 250 ++++++++++++++++++++++++
 12 files changed, 849 insertions(+)
 create mode 100644 projects/led_blinker/app/index.html
 create mode 100755 projects/led_blinker/app/start.sh
 create mode 100644 projects/led_blinker/block_design.tcl
 create mode 100644 scripts/bitstream.tcl
 create mode 100644 scripts/core.tcl
 create mode 100755 scripts/debian.sh
 create mode 100644 scripts/devicetree.tcl
 create mode 100644 scripts/fsbl.tcl
 create mode 100644 scripts/hwdef.tcl
 create mode 100755 scripts/image.sh
 create mode 100644 scripts/project.tcl
 create mode 100755 scripts/ubuntu.sh

diff --git a/projects/led_blinker/app/index.html b/projects/led_blinker/app/index.html
new file mode 100644
index 0000000..11f7dfe
--- /dev/null
+++ b/projects/led_blinker/app/index.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>LED blinker</title>
+<link rel="stylesheet" href="/css/main.css">
+<link rel="stylesheet" href="/css/pygments.css">
+</head>
+<body>
+<div id="header">
+<div id="logo">
+<a href="/">Back to all apps</a>
+</div>
+<div id="menu">
+<a href="https://github.com/pavel-demin/red-pitaya-notes" target="_blank">Source</a>
+<a href="https://github.com/pavel-demin/red-pitaya-notes/issues" target="_blank">Issues</a>
+</div>
+</div>
+<div id="content">
+<h1>LED blinker</h1>
+<p>The LED blinker application is ready.</p>
+</div>
+</body>
+</html>
diff --git a/projects/led_blinker/app/start.sh b/projects/led_blinker/app/start.sh
new file mode 100755
index 0000000..c2cb31d
--- /dev/null
+++ b/projects/led_blinker/app/start.sh
@@ -0,0 +1,7 @@
+#! /bin/sh
+
+apps_dir=/media/mmcblk0p1/apps
+
+source $apps_dir/stop.sh
+
+cat $apps_dir/led_blinker/led_blinker.bit > /dev/xdevcfg
diff --git a/projects/led_blinker/block_design.tcl b/projects/led_blinker/block_design.tcl
new file mode 100644
index 0000000..f20c218
--- /dev/null
+++ b/projects/led_blinker/block_design.tcl
@@ -0,0 +1,55 @@
+source projects/base_system/block_design.tcl
+
+# Create xlconstant
+cell xilinx.com:ip:xlconstant const_0
+
+# Create proc_sys_reset
+cell xilinx.com:ip:proc_sys_reset rst_0 {} {
+  ext_reset_in const_0/dout
+}
+
+# LED
+
+# Create c_counter_binary
+cell xilinx.com:ip:c_counter_binary cntr_0 {
+  Output_Width 32
+} {
+  CLK pll_0/clk_out1
+}
+
+# Create port_slicer
+cell labdpr:user:port_slicer slice_0 {
+  DIN_WIDTH 32 DIN_FROM 26 DIN_TO 26
+} {
+  din cntr_0/Q
+  dout led_o
+}
+
+# STS
+
+# Create dna_reader
+cell labdpr:user:dna_reader dna_0 {} {
+  aclk pll_0/clk_out1
+  aresetn rst_0/peripheral_aresetn
+}
+
+# Create xlconcat
+cell xilinx.com:ip:xlconcat concat_0 {
+  NUM_PORTS 2
+  IN0_WIDTH 32
+  IN1_WIDTH 64
+} {
+  In0 const_0/dout
+  In1 dna_0/dna_data
+}
+
+# Create axi_sts_register
+cell labdpr:user:axi_sts_register sts_0 {
+  STS_DATA_WIDTH 96
+  AXI_ADDR_WIDTH 32
+  AXI_DATA_WIDTH 32
+} {
+  sts_data concat_0/dout
+}
+
+addr 0x40000000 4K sts_0/S_AXI /ps_0/M_AXI_GP0
diff --git a/scripts/bitstream.tcl b/scripts/bitstream.tcl
new file mode 100644
index 0000000..5991488
--- /dev/null
+++ b/scripts/bitstream.tcl
@@ -0,0 +1,17 @@
+
+set project_name [lindex $argv 0]
+
+open_project tmp/$project_name.xpr
+
+if {[get_property PROGRESS [get_runs impl_1]] != "100%"} {
+  launch_runs impl_1 -to_step route_design
+  wait_on_run impl_1
+}
+
+open_run [get_runs impl_1]
+
+set_property BITSTREAM.GENERAL.COMPRESS TRUE [current_design]
+
+write_bitstream -force -file tmp/$project_name.bit
+
+close_project
diff --git a/scripts/core.tcl b/scripts/core.tcl
new file mode 100644
index 0000000..b8d56ff
--- /dev/null
+++ b/scripts/core.tcl
@@ -0,0 +1,48 @@
+
+set core_name [lindex $argv 0]
+
+set part_name [lindex $argv 1]
+
+set elements [split $core_name _]
+set project_name [join [lrange $elements 0 end-2] _]
+set version [string trimleft [join [lrange $elements end-1 end] .] v]
+
+file delete -force tmp/cores/$core_name tmp/cores/$project_name.cache tmp/cores/$project_name.hw tmp/cores/$project_name.xpr tmp/cores/$project_name.ip_user_files
+
+create_project -part $part_name $project_name tmp/cores
+
+add_files -norecurse [glob cores/$core_name/*.vhd]
+
+ipx::package_project -import_files -root_dir tmp/cores/$core_name
+
+set core [ipx::current_core]
+
+set_property VERSION $version $core
+set_property NAME $project_name $core
+set_property LIBRARY {user} $core
+set_property VENDOR {labdpr} $core
+set_property VENDOR_DISPLAY_NAME {labdpr} $core
+set_property COMPANY_URL {http://labdpr.cab.cnea.gov.ar/} $core
+set_property SUPPORTED_FAMILIES {zynq Production} $core
+
+proc core_parameter {name display_name description} {
+  set core [ipx::current_core]
+
+  set parameter [ipx::get_user_parameters $name -of_objects $core]
+  set_property DISPLAY_NAME $display_name $parameter
+  set_property DESCRIPTION $description $parameter
+
+  set parameter [ipgui::get_guiparamspec -name $name -component $core]
+  set_property DISPLAY_NAME $display_name $parameter
+  set_property TOOLTIP $description $parameter
+}
+
+source cores/$core_name/core_config.tcl
+
+rename core_parameter {}
+
+ipx::create_xgui_files $core
+ipx::update_checksums $core
+ipx::save_core $core
+
+close_project
diff --git a/scripts/debian.sh b/scripts/debian.sh
new file mode 100755
index 0000000..8e30075
--- /dev/null
+++ b/scripts/debian.sh
@@ -0,0 +1,267 @@
+device=$1
+
+boot_dir=`mktemp -d /tmp/BOOT.XXXXXXXXXX`
+root_dir=`mktemp -d /tmp/ROOT.XXXXXXXXXX`
+
+linux_dir=tmp/linux-5.10
+linux_ver=5.10.107-xilinx
+
+# Choose mirror automatically, depending the geographic and network location
+mirror=http://deb.debian.org/debian
+
+distro=buster
+arch=armhf
+
+passwd=escondido
+timezone=America/Argentina/Mendoza
+
+# Create partitions
+
+parted -s $device mklabel msdos
+parted -s $device mkpart primary fat16 4MiB 16MiB
+parted -s $device mkpart primary ext4 16MiB 100%
+
+boot_dev=/dev/`lsblk -ln -o NAME -x NAME $device | sed '2!d'`
+root_dev=/dev/`lsblk -ln -o NAME -x NAME $device | sed '3!d'`
+
+# Create file systems
+
+mkfs.vfat -v $boot_dev
+mkfs.ext4 -F -j $root_dev
+
+# Mount file systems
+
+mount $boot_dev $boot_dir
+mount $root_dev $root_dir
+
+# Copy files to the boot file system
+
+cp boot.bin devicetree.dtb uImage $boot_dir
+cp uEnv-ext4.txt $boot_dir/uEnv.txt
+
+# Install Debian base system to the root file system
+
+debootstrap --foreign --arch $arch $distro $root_dir $mirror
+
+# Install Linux modules
+
+modules_dir=$root_dir/lib/modules/$linux_ver
+
+mkdir -p $modules_dir/kernel
+
+find $linux_dir -name \*.ko -printf '%P\0' | tar --directory=$linux_dir --owner=0 --group=0 --null --files-from=- -zcf - | tar -zxf - --directory=$modules_dir/kernel
+
+cp $linux_dir/modules.order $linux_dir/modules.builtin $modules_dir/
+
+depmod -a -b $root_dir $linux_ver
+
+# Add missing configuration files and packages
+
+cp /etc/resolv.conf $root_dir/etc/
+cp /usr/bin/qemu-arm-static $root_dir/usr/bin/
+
+chroot $root_dir <<- EOF_CHROOT
+export LANG=C
+export LC_ALL=C
+
+export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+
+/debootstrap/debootstrap --second-stage
+
+cat <<- EOF_CAT > /etc/apt/sources.list
+deb $mirror $distro main contrib non-free
+deb-src $mirror $distro main contrib non-free
+deb $mirror $distro-updates main contrib non-free
+deb-src $mirror $distro-updates main contrib non-free
+deb http://security.debian.org/debian-security $distro/updates main contrib non-free
+deb-src http://security.debian.org/debian-security $distro/updates main contrib non-free
+EOF_CAT
+
+cat <<- EOF_CAT > etc/apt/apt.conf.d/99norecommends
+APT::Install-Recommends "0";
+APT::Install-Suggests "0";
+EOF_CAT
+
+cat <<- EOF_CAT > etc/fstab
+# /etc/fstab: static file system information.
+# <file system> <mount point>   <type>  <options>           <dump>  <pass>
+/dev/mmcblk0p2  /               ext4    errors=remount-ro   0       1
+/dev/mmcblk0p1  /boot           vfat    defaults            0       2
+EOF_CAT
+
+echo red-pitaya > etc/hostname
+
+apt-get update
+apt-get -y upgrade
+
+apt-get -y install locales
+
+sed -i "/^# en_US.UTF-8 UTF-8$/s/^# //" etc/locale.gen
+locale-gen
+update-locale LANG=en_US.UTF-8
+
+ln -sf /usr/share/zoneinfo/$timezone etc/localtime
+dpkg-reconfigure --frontend=noninteractive tzdata
+
+apt-get -y install openssh-server ca-certificates ntp ntpdate fake-hwclock \
+  usbutils psmisc lsof parted curl vim wpasupplicant hostapd isc-dhcp-server \
+  iw firmware-realtek firmware-ralink firmware-atheros firmware-brcm80211 \
+  ifplugd ntfs-3g net-tools less
+
+sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/' etc/ssh/sshd_config
+
+cat <<- EOF_CAT >> etc/securetty
+
+# Serial Console for Xilinx Zynq-7000
+ttyPS0
+EOF_CAT
+
+touch etc/udev/rules.d/80-net-setup-link.rules
+
+cat <<- EOF_CAT > etc/network/interfaces.d/eth0
+iface eth0 inet dhcp
+EOF_CAT
+
+cat <<- EOF_CAT > etc/default/ifplugd
+INTERFACES="eth0"
+HOTPLUG_INTERFACES=""
+ARGS="-q -f -u0 -d10 -w -I"
+SUSPEND_ACTION="stop"
+EOF_CAT
+
+cat <<- EOF_CAT > etc/network/interfaces.d/wlan0
+allow-hotplug wlan0
+iface wlan0 inet static
+  address 192.168.42.1
+  netmask 255.255.255.0
+  post-up service hostapd restart
+  post-up service isc-dhcp-server restart
+  post-up iptables-restore < /etc/iptables.ipv4.nat
+  pre-down iptables-restore < /etc/iptables.ipv4.nonat
+  pre-down service isc-dhcp-server stop
+  pre-down service hostapd stop
+EOF_CAT
+
+cat <<- EOF_CAT > etc/hostapd/hostapd.conf
+interface=wlan0
+ssid=RedPitaya
+driver=nl80211
+hw_mode=g
+channel=6
+macaddr_acl=0
+auth_algs=1
+ignore_broadcast_ssid=0
+wpa=2
+wpa_passphrase=RedPitaya
+wpa_key_mgmt=WPA-PSK
+wpa_pairwise=CCMP
+rsn_pairwise=CCMP
+EOF_CAT
+
+cat <<- EOF_CAT > etc/default/hostapd
+DAEMON_CONF=/etc/hostapd/hostapd.conf
+EOF_CAT
+
+cat <<- EOF_CAT > etc/default/isc-dhcp-server
+INTERFACESv4=wlan0
+EOF_CAT
+
+cat <<- EOF_CAT > etc/dhcp/dhcpd.conf
+ddns-update-style none;
+default-lease-time 600;
+max-lease-time 7200;
+authoritative;
+log-facility local7;
+subnet 192.168.42.0 netmask 255.255.255.0 {
+  range 192.168.42.10 192.168.42.50;
+  option broadcast-address 192.168.42.255;
+  option routers 192.168.42.1;
+  default-lease-time 600;
+  max-lease-time 7200;
+  option domain-name "local";
+  option domain-name-servers 8.8.8.8, 8.8.4.4;
+}
+EOF_CAT
+
+cat <<- EOF_CAT >> etc/dhcp/dhclient.conf
+timeout 20;
+
+lease {
+  interface "eth0";
+  fixed-address 192.168.1.100;
+  option subnet-mask 255.255.255.0;
+  renew 2 2030/1/1 00:00:01;
+  rebind 2 2030/1/1 00:00:01;
+  expire 2 2030/1/1 00:00:01;
+}
+EOF_CAT
+
+sed -i '/^#net.ipv4.ip_forward=1$/s/^#//' etc/sysctl.conf
+
+cat <<- EOF_CAT > etc/iptables.ipv4.nat
+*nat
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+-A POSTROUTING -o eth0 -j MASQUERADE
+COMMIT
+*mangle
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*filter
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
+-A FORWARD -i wlan0 -o eth0 -j ACCEPT
+COMMIT
+EOF_CAT
+
+cat <<- EOF_CAT > etc/iptables.ipv4.nonat
+*nat
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*mangle
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*filter
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+COMMIT
+EOF_CAT
+
+apt-get clean
+
+echo root:$passwd | chpasswd
+
+service ntp stop
+service ssh stop
+
+history -c
+
+sync
+EOF_CHROOT
+
+rm $root_dir/etc/resolv.conf
+rm $root_dir/usr/bin/qemu-arm-static
+
+# Unmount file systems
+
+umount $boot_dir $root_dir
+
+rmdir $boot_dir $root_dir
+
+zerofree $root_dev
diff --git a/scripts/devicetree.tcl b/scripts/devicetree.tcl
new file mode 100644
index 0000000..f5bc140
--- /dev/null
+++ b/scripts/devicetree.tcl
@@ -0,0 +1,27 @@
+
+set project_name [lindex $argv 0]
+
+set proc_name [lindex $argv 1]
+
+set repo_path [lindex $argv 2]
+
+set boot_args {console=ttyPS0,115200 earlyprintk}
+
+set hard_path tmp/$project_name.hard
+set tree_path tmp/$project_name.tree
+
+file mkdir $hard_path
+file copy -force tmp/$project_name.xsa $hard_path/$project_name.xsa
+
+hsi set_repo_path $repo_path
+
+hsi open_hw_design $hard_path/$project_name.xsa
+hsi create_sw_design -proc $proc_name -os device_tree devicetree
+
+hsi set_property CONFIG.kernel_version {2020.2} [hsi get_os]
+hsi set_property CONFIG.bootargs $boot_args [hsi get_os]
+
+hsi generate_target -dir $tree_path
+
+hsi close_sw_design [hsi current_sw_design]
+hsi close_hw_design [hsi current_hw_design]
diff --git a/scripts/fsbl.tcl b/scripts/fsbl.tcl
new file mode 100644
index 0000000..64095ab
--- /dev/null
+++ b/scripts/fsbl.tcl
@@ -0,0 +1,20 @@
+
+set project_name [lindex $argv 0]
+
+set proc_name [lindex $argv 1]
+
+set hard_path tmp/$project_name.hard
+set fsbl_path tmp/$project_name.fsbl
+
+file mkdir $hard_path
+file copy -force tmp/$project_name.xsa $hard_path/$project_name.xsa
+
+hsi open_hw_design $hard_path/$project_name.xsa
+hsi create_sw_design -proc $proc_name -os standalone fsbl
+
+hsi add_library xilffs
+hsi add_library xilrsa
+
+hsi generate_app -proc $proc_name -app zynq_fsbl -dir $fsbl_path -compile
+
+hsi close_hw_design [hsi current_hw_design]
diff --git a/scripts/hwdef.tcl b/scripts/hwdef.tcl
new file mode 100644
index 0000000..8adeaba
--- /dev/null
+++ b/scripts/hwdef.tcl
@@ -0,0 +1,8 @@
+
+set project_name [lindex $argv 0]
+
+open_project tmp/$project_name.xpr
+
+write_hw_platform -fixed -force -file tmp/$project_name.xsa
+
+close_project
diff --git a/scripts/image.sh b/scripts/image.sh
new file mode 100755
index 0000000..6a8cffc
--- /dev/null
+++ b/scripts/image.sh
@@ -0,0 +1,19 @@
+script=$1
+image=$2
+
+size=512
+
+if [ $# -eq 3 ]
+then
+  size=$3
+fi
+
+dd if=/dev/zero of=$image bs=1M count=$size status=progress
+
+device=`losetup -f`
+
+losetup $device $image
+
+sh $script $device
+
+losetup -d $device
diff --git a/scripts/project.tcl b/scripts/project.tcl
new file mode 100644
index 0000000..5d1a181
--- /dev/null
+++ b/scripts/project.tcl
@@ -0,0 +1,107 @@
+
+set project_name [lindex $argv 0]
+
+set part_name [lindex $argv 1]
+
+file delete -force tmp/$project_name.cache tmp/$project_name.hw tmp/$project_name.srcs tmp/$project_name.runs tmp/$project_name.sim tmp/$project_name.ip_user_files tmp/$project_name.xpr
+
+create_project -part $part_name $project_name tmp
+
+set_property IP_REPO_PATHS tmp/cores [current_project]
+
+update_ip_catalog
+
+set_property target_language VHDL [current_project]
+
+set bd_path tmp/$project_name.srcs/sources_1/bd/system
+
+create_bd_design system
+
+source cfg/ports.tcl
+
+proc cell {cell_vlnv cell_name {cell_props {}} {cell_ports {}}} {
+  set cell [create_bd_cell -type ip -vlnv $cell_vlnv $cell_name]
+  set prop_list {}
+  foreach {prop_name prop_value} [uplevel 1 [list subst $cell_props]] {
+    lappend prop_list CONFIG.$prop_name $prop_value
+  }
+  if {[llength $prop_list] > 1} {
+    set_property -dict $prop_list $cell
+  }
+  foreach {local_name remote_name} [uplevel 1 [list subst $cell_ports]] {
+    set local_port [get_bd_pins $cell_name/$local_name]
+    set remote_port [get_bd_pins $remote_name]
+    if {[llength $local_port] == 1 && [llength $remote_port] == 1} {
+      connect_bd_net $local_port $remote_port
+      continue
+    }
+    set local_port [get_bd_intf_pins $cell_name/$local_name]
+    set remote_port [get_bd_intf_pins $remote_name]
+    if {[llength $local_port] == 1 && [llength $remote_port] == 1} {
+      connect_bd_intf_net $local_port $remote_port
+      continue
+    }
+    error "** ERROR: can't connect $cell_name/$local_name and $remote_name"
+  }
+}
+
+proc module {module_name module_body {module_ports {}}} {
+  set bd [current_bd_instance .]
+  current_bd_instance [create_bd_cell -type hier $module_name]
+  eval $module_body
+  current_bd_instance $bd
+  foreach {local_name remote_name} [uplevel 1 [list subst $module_ports]] {
+    set local_port [get_bd_pins $module_name/$local_name]
+    set remote_port [get_bd_pins $remote_name]
+    if {[llength $local_port] == 1 && [llength $remote_port] == 1} {
+      connect_bd_net $local_port $remote_port
+      continue
+    }
+    set local_port [get_bd_intf_pins $module_name/$local_name]
+    set remote_port [get_bd_intf_pins $remote_name]
+    if {[llength $local_port] == 1 && [llength $remote_port] == 1} {
+      connect_bd_intf_net $local_port $remote_port
+      continue
+    }
+    error "** ERROR: can't connect $module_name/$local_name and $remote_name"
+  }
+}
+
+proc addr {offset range port master} {
+  set object [get_bd_intf_pins $port]
+  set segment [get_bd_addr_segs -of_objects $object]
+  set config [list Master $master Clk Auto]
+  apply_bd_automation -rule xilinx.com:bd_rule:axi4 -config $config $object
+  assign_bd_address -offset $offset -range $range $segment
+}
+
+source projects/$project_name/block_design.tcl
+
+rename cell {}
+rename module {}
+rename addr {}
+
+if {[version -short] >= 2016.3} {
+  set_property synth_checkpoint_mode None [get_files $bd_path/system.bd]
+}
+
+generate_target all [get_files $bd_path/system.bd]
+make_wrapper -files [get_files $bd_path/system.bd] -top
+
+add_files -norecurse $bd_path/hdl/system_wrapper.vhd
+
+set files [glob -nocomplain projects/$project_name/*.vhd]
+if {[llength $files] > 0} {
+  add_files -norecurse $files
+}
+
+set files [glob -nocomplain cfg/*.xdc projects/$project_name/*.xdc]
+if {[llength $files] > 0} {
+  add_files -norecurse -fileset constrs_1 $files
+}
+
+
+set_property STRATEGY Flow_PerfOptimized_high [get_runs synth_1]
+set_property STRATEGY Performance_NetDelay_high [get_runs impl_1]
+
+close_project
diff --git a/scripts/ubuntu.sh b/scripts/ubuntu.sh
new file mode 100755
index 0000000..e877eb9
--- /dev/null
+++ b/scripts/ubuntu.sh
@@ -0,0 +1,250 @@
+device=$1
+
+boot_dir=`mktemp -d /tmp/BOOT.XXXXXXXXXX`
+root_dir=`mktemp -d /tmp/ROOT.XXXXXXXXXX`
+
+linux_dir=tmp/linux-5.10
+linux_ver=5.10.46-xilinx
+
+root_tar=ubuntu-base-20.04.2-base-armhf.tar.gz
+root_url=http://cdimage.ubuntu.com/ubuntu-base/releases/20.04/release/$root_tar
+
+passwd=escondido
+timezone=America/Argentina/Mendoza
+
+# Create partitions
+
+parted -s $device mklabel msdos
+parted -s $device mkpart primary fat16 4MiB 16MiB
+parted -s $device mkpart primary ext4 16MiB 100%
+
+boot_dev=/dev/`lsblk -ln -o NAME -x NAME $device | sed '2!d'`
+root_dev=/dev/`lsblk -ln -o NAME -x NAME $device | sed '3!d'`
+
+# Create file systems
+
+mkfs.vfat -v $boot_dev
+mkfs.ext4 -F -j $root_dev
+
+# Mount file systems
+
+mount $boot_dev $boot_dir
+mount $root_dev $root_dir
+
+# Copy files to the boot file system
+
+cp boot.bin devicetree.dtb uImage $boot_dir
+cp uEnv-ext4.txt $boot_dir/uEnv.txt
+
+# Copy Ubuntu Core to the root file system
+
+test -f $root_tar || curl -L $root_url -o $root_tar
+
+tar -zxf $root_tar --directory=$root_dir
+
+# Install Linux modules
+
+modules_dir=$root_dir/lib/modules/$linux_ver
+
+mkdir -p $modules_dir/kernel
+
+find $linux_dir -name \*.ko -printf '%P\0' | tar --directory=$linux_dir --owner=0 --group=0 --null --files-from=- -zcf - | tar -zxf - --directory=$modules_dir/kernel
+
+cp $linux_dir/modules.order $linux_dir/modules.builtin $modules_dir/
+
+depmod -a -b $root_dir $linux_ver
+
+# Add missing configuration files and packages
+
+cp /etc/resolv.conf $root_dir/etc/
+cp /usr/bin/qemu-arm-static $root_dir/usr/bin/
+
+chroot $root_dir <<- EOF_CHROOT
+export LANG=C
+export LC_ALL=C
+
+export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+
+cat <<- EOF_CAT > etc/apt/apt.conf.d/99norecommends
+APT::Install-Recommends "0";
+APT::Install-Suggests "0";
+EOF_CAT
+
+cat <<- EOF_CAT > etc/fstab
+# /etc/fstab: static file system information.
+# <file system> <mount point>   <type>  <options>           <dump>  <pass>
+/dev/mmcblk0p2  /               ext4    errors=remount-ro   0       1
+/dev/mmcblk0p1  /boot           vfat    defaults            0       2
+EOF_CAT
+
+cat <<- EOF_CAT >> etc/securetty
+
+# Serial Console for Xilinx Zynq-7000
+ttyPS0
+EOF_CAT
+
+sed 's/tty1/ttyPS0/g; s/38400/115200/' etc/init/tty1.conf > etc/init/ttyPS0.conf
+
+echo red-pitaya > etc/hostname
+
+sed -i '/^# deb .* universe$/s/^# //' etc/apt/sources.list
+
+sed -i '/### END INIT INFO/aexit 0' /etc/init.d/udev
+apt-get update
+apt-get -y upgrade
+sed -i '/### END INIT INFO/{n;d}' /etc/init.d/udev
+
+apt-get -y install locales
+
+locale-gen en_US.UTF-8
+update-locale LANG=en_US.UTF-8
+
+echo $timezone > etc/timezone
+dpkg-reconfigure --frontend=noninteractive tzdata
+
+apt-get -y install openssh-server ca-certificates ntp usbutils psmisc lsof \
+  parted curl less vim man-db iw wpasupplicant linux-firmware ntfs-3g
+
+sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/' etc/ssh/sshd_config
+
+apt-get -y install hostapd isc-dhcp-server iptables
+
+touch etc/udev/rules.d/75-persistent-net-generator.rules
+
+cat <<- EOF_CAT >> etc/network/interfaces.d/eth0
+allow-hotplug eth0
+iface eth0 inet dhcp
+EOF_CAT
+
+cat <<- EOF_CAT > etc/network/interfaces.d/wlan0
+allow-hotplug wlan0
+iface wlan0 inet static
+  address 192.168.42.1
+  netmask 255.255.255.0
+  post-up service hostapd restart
+  post-up service isc-dhcp-server restart
+  post-up iptables-restore < /etc/iptables.ipv4.nat
+  pre-down iptables-restore < /etc/iptables.ipv4.nonat
+  pre-down service isc-dhcp-server stop
+  pre-down service hostapd stop
+EOF_CAT
+
+cat <<- EOF_CAT > etc/hostapd/hostapd.conf
+interface=wlan0
+ssid=RedPitaya
+driver=nl80211
+hw_mode=g
+channel=6
+macaddr_acl=0
+auth_algs=1
+ignore_broadcast_ssid=0
+wpa=2
+wpa_passphrase=RedPitaya
+wpa_key_mgmt=WPA-PSK
+wpa_pairwise=CCMP
+rsn_pairwise=CCMP
+EOF_CAT
+
+cat <<- EOF_CAT > etc/default/hostapd
+DAEMON_CONF=/etc/hostapd/hostapd.conf
+EOF_CAT
+
+cat <<- EOF_CAT > etc/dhcp/dhcpd.conf
+ddns-update-style none;
+default-lease-time 600;
+max-lease-time 7200;
+authoritative;
+log-facility local7;
+subnet 192.168.42.0 netmask 255.255.255.0 {
+  range 192.168.42.10 192.168.42.50;
+  option broadcast-address 192.168.42.255;
+  option routers 192.168.42.1;
+  default-lease-time 600;
+  max-lease-time 7200;
+  option domain-name "local";
+  option domain-name-servers 8.8.8.8, 8.8.4.4;
+}
+EOF_CAT
+
+cat <<- EOF_CAT >> etc/dhcp/dhclient.conf
+timeout 20;
+
+lease {
+  interface "eth0";
+  fixed-address 192.168.1.100;
+  option subnet-mask 255.255.255.0;
+  renew 2 2030/1/1 00:00:01;
+  rebind 2 2030/1/1 00:00:01;
+  expire 2 2030/1/1 00:00:01;
+}
+EOF_CAT
+
+sed -i '/^#net.ipv4.ip_forward=1$/s/^#//' etc/sysctl.conf
+
+cat <<- EOF_CAT > etc/iptables.ipv4.nat
+*nat
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+-A POSTROUTING -o eth0 -j MASQUERADE
+COMMIT
+*mangle
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*filter
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
+-A FORWARD -i wlan0 -o eth0 -j ACCEPT
+COMMIT
+EOF_CAT
+
+cat <<- EOF_CAT > etc/iptables.ipv4.nonat
+*nat
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*mangle
+:PREROUTING ACCEPT [0:0]
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+:POSTROUTING ACCEPT [0:0]
+COMMIT
+*filter
+:INPUT ACCEPT [0:0]
+:FORWARD ACCEPT [0:0]
+:OUTPUT ACCEPT [0:0]
+COMMIT
+EOF_CAT
+
+apt-get clean
+
+echo root:$passwd | chpasswd
+
+service ntp stop
+service ssh stop
+
+history -c
+
+sync
+EOF_CHROOT
+
+rm $root_dir/etc/resolv.conf
+rm $root_dir/usr/bin/qemu-arm-static
+
+# Unmount file systems
+
+umount $boot_dir $root_dir
+
+rmdir $boot_dir $root_dir
+
+zerofree $root_dev
-- 
GitLab