#!/bin/bash

#  helper script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014, 2015 Francis Rowe <info@gluglug.org.uk>
#	Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# This script assumes that the working directory is the
# root of libreboot_src or libreboot git.

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

printf "Downloading coreboot, patching coreboot and deblobbing coreboot\n"

# This grabs current base used, and applies patches
# This is also used to run the deblob scripts.

# Remove the old version that may exist
# ------------------------------------------------------------------------------

rm -Rf "coreboot/"

# Get latest coreboot:
# ------------------------------------------------------------------------------

# download it using git
git clone http://review.coreboot.org/coreboot

# there are modifications required
cd "coreboot/"

# reset to previously tested revision
git reset --hard 33fb4cf0ffb01be8bcb6b488872c87eb50e7d77f

# vboot submodule is needed
git submodule update --init --checkout -- 3rdparty/vboot/

# there are modifications required
cd "3rdparty/vboot/"

# reset vboot to last known good revision
git reset --hard fbf631c845c08299f0bcbae3f311c5807d34c0d6

# Patch vboot
# ------------------------------------------------------------------------------

printf "firmware: Developer mode timeout delay shortening (down to 3 seconds)\n"
git am "../../../resources/libreboot/patch/vboot/0001-firmware-Developer-mode-timeout-delay-shortening-dow.patch"

printf "firmware: Text-based screen display in priority\n"
git am "../../../resources/libreboot/patch/vboot/0002-firmware-Text-based-screen-display-in-priority.patch"

printf "firmware: NV context pointer handoff to VbExDisplayScreen\n"
git am "../../../resources/libreboot/patch/vboot/0003-firmware-NV-context-pointer-handoff-to-VbExDisplaySc.patch"

printf "firmware: Hold key combination in developer mode\n"
git am "../../../resources/libreboot/patch/vboot/0004-firmware-Hold-key-combination-in-developer-mode.patch"

printf "firmware: Screen blank and wait at disabled USB boot warning\n"
git am "../../../resources/libreboot/patch/vboot/0005-firmware-Screen-blank-and-wait-at-disabled-USB-boot-.patch"

printf "firmware: Separate screen and wait at device information screen\n"
git am "../../../resources/libreboot/patch/vboot/0006-firmware-Separate-screen-and-wait-at-device-informat.patch"

printf "firmware: Localization keys removal\n"
git am "../../../resources/libreboot/patch/vboot/0007-firmware-Localization-keys-removal.patch"

# leave the vboot tree
cd "../../"


# Get patches from review.coreboot.org
# ------------------------------------------------------------------------------

printf "mainboard/lenovo/t400: Add initial hybrid graphics support\n"
git am "../resources/libreboot/patch/misc/0001-mainboard-lenovo-t400-Add-initial-hybrid-graphics-su.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/19/9319/18 && git cherry-pick FETCH_HEAD

# not included, but keep an eye on it:
# printf "mainboard/lenovo/t400: Increase backlight frequency to reduce flicker\n"
# git fetch http://review.coreboot.org/coreboot refs/changes/31/9331/14 && git cherry-pick FETCH_HEAD

printf "NOTFORMERGE: lenovo/t400: hard-code enable integrated-only video\n"
git am "../resources/libreboot/patch/misc/0002-NOTFORMERGE-lenovo-t400-hard-code-enable-integrated-.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/50/10550/1 && git cherry-pick FETCH_HEAD

printf "lenovo/x60: use correct BLC_PWM_CTL value\n"
git am "../resources/libreboot/patch/misc/0003-lenovo-x60-use-correct-BLC_PWM_CTL-value.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/24/10624/2 && git cherry-pick FETCH_HEAD

printf "lenovo/t60: Enable brightness controls (native graphics)\n"
git am "../resources/libreboot/patch/misc/0004-lenovo-t60-Enable-brightness-controls-native-graphic.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/52/10552/2 && git cherry-pick FETCH_HEAD

printf "ec/lenovo/h8: permanently enable wifi/trackpoint/touchpad/bluetooth/wwan\n"
git am "../resources/libreboot/patch/misc/0005-NOTFORMERGE-ec-lenovo-h8-wlan-trackpoint-touchpad-bl.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/58/7058/9 && git cherry-pick FETCH_HEAD

printf "northbridge/gm45/raminit.c: enable GS45 high-perf (i.e. add X200S support to libreboot)\n"
git am "../resources/libreboot/patch/misc/0006-northbridge-gm45-raminit.c-enable-GS45-high-performa.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/35/11135/3 && git cherry-pick FETCH_HEAD

# Patch removed for now, affected by this patch:
# http://review.coreboot.org/#/c/11702/
# printf "fix uneven backlight on X200 (when setting brightness low)\n"
# git fetch http://review.coreboot.org/coreboot refs/changes/79/7979/2 && git cherry-pick FETCH_HEAD

printf "ThinkPad R400 support (clone of the T400)\n"
git am "../resources/libreboot/patch/misc/0007-lenovo-r400-Add-clone-of-Lenovo-T400.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/93/8393/5 && git cherry-pick FETCH_HEAD

printf "ThinkPad T500 (depends on T400 patch)\n"
git am "../resources/libreboot/patch/misc/0008-lenovo-t500-Add-clone-of-Lenovo-T400.patch"
# git fetch http://review.coreboot.org/coreboot refs/changes/45/10545/1 && git cherry-pick FETCH_HEAD

# CrOS:

printf "chromeos: Allow disabling vboot firmware verification when ChromeOS is enabled\n"
git am "../resources/libreboot/patch/cros/0001-chromeos-Allow-disabling-vboot-firmware-verification.patch"

# KGPE-D16 patches
# new versions can be found at https://raptorengineeringinc.com/coreboot/kgpe-d16-status.php
for i in ../resources/libreboot/patch/kgpe-d16/*; do
	git am "${i}"
done
# Temporary fix (TODO: get tpearson to fix properly):
# Remove code from coreboot that adds microcode updates
# git fetch http://review.coreboot.org/coreboot refs/changes/90/12090/1 && git cherry-pick FETCH_HEAD
git am "../resources/libreboot/patch/tmpfix/0001-NOTFORMERGE-don-t-add-CPU-microcode-on-fam10h-to-fam.patch"

# Run coreboot-libre deblob scripts
# ------------------------------------------------------------------------------

printf "Deleting .git* in coreboot/ (history inside .git contains the blobs that were deleted)\n"
rm -Rf ".git/"
rm -f ".gitreview"
rm -f ".gitmodules"
rm -f ".gitignore"

rm -Rf 3rdparty/*/.git*

cd "../"

printf "Deblobbing coreboot\n"
./resources/utilities/coreboot-libre/deblob

if [ -f "version" ]; then
	# _src release archive is being used
	version="libreboot-$(cat version)"
else
	# git repo is being used
	version="libreboot-$(git describe --tags HEAD)"
fi
printf '%s\n' "${version}" >"coreboot/.coreboot-version"

printf "\n\n"