#!/bin/bash

#  getcb script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014 Francis Rowe
#
#    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 grabs current base used, and applies patches
# This is also used to run deblob scripts.

# (the idea is that this script will reproduce the coreboot directory included with this version of libreboot)

# You need the dependencies from ./builddeb to use this script.

#
# The deblobbing target (coreboot git revision) for coreboot-libre is
# the following commit in the coreboot git repository named "master":
#
# commit 63acd22dc5366c72a7165138f5030df9523824dc
# Author: Vladimir Serbinenko <phcoder@gmail.com>
# Date:   Sun Jun 1 00:26:48 2014 +0200
# 
#     lenovo: Make version look like something thinkpad_acpi would accept
#     
#     thinkpad_acpi checks that BIOS version matches some pattern.
#     Report version in this form.
#     
#     Not cleaned up as the idea of this patch seems to be met with resistance.
#     Can make it Thinkpad-specific if the idea is accepted.
#     
#     Change-Id: I15e33e87e7a7f42d6a06f12fb39b5172153af8a1
#     Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
#     Reviewed-on: http://review.coreboot.org/4650
#     Tested-by: build bot (Jenkins)
#     Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
#

#
# We do not need to do a "git reset --hard 63acd22dc5366c72a7165138f5030df9523824dc" here
# because 5324/9 checkout (see below) eventually goes down to dependency 5320/9
# which is checked out on top of 63acd22dc5366c72a7165138f5030df9523824dc
#

#
# Use "git log" in coreboot after this is done, to see what was done.
# Also use "git diff" to see uncommited changes (after the patch was applied by this script)
#

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

echo "DOWNLOADING AND PATCHING COREBOOT"

rm -rf coreboot

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

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

# there are modifications required
cd coreboot

# Get patches for "native graphics initialization" on X60 and T60
# ----------------------------------------------------------------------------------

# CHECKOUT (the XXXX/Y numbers are gerrit/git):
# Checks out the following from the gerrit site at http://review.coreboot.org/
# 5324/9 review.coreboot.org (not merged in cb master on day 20140630) - apple/macbook21: Fix audio.
# 5323/9 review.coreboot.org (not merged in cb master on day 20140630) - apple/macbook21: EC handline ACPI implementation.
# 5321/14 review.coreboot.org (not merged in cb master on day 20140630) - A new port apple/macbook21. (new macbook21 port + native graphics)
# 5320/9 review.coreboot.org (not merged in cb master on day 20140630) - i945: Replace video gfx init. (X60 native graphics)
# 5320/9 is the end (locically). It's dependency 4650/29 was merged since day 20140601 (and included already in this libreboot tree, even).
# 4650/29 (merged in cb master on 20140601) - lenovo: Make version look like something thinkpad_acpi would accept
git fetch http://review.coreboot.org/coreboot refs/changes/24/5324/9 && git checkout FETCH_HEAD
 
# CHERRY PICK (the XXXX/Y numbers are gerrit/git):
# Cherry picks the following from the gerrit site at http://review.coreboot.org/
# 5345/4 review.coreboot.org (not merged in cb master on day 20140630) - lenovo/t60: Enable native intel gfx init.
# 5345//4 is the end (logically). It's dependency 5344/4, or rather 5344/5 was merged in cb master on 20140601 (and included in this libreboot tree, even)
# Note: 5345/4 is based on the older 5344/4 before the merge so isn't rebased at the time of writing.
git fetch http://review.coreboot.org/coreboot refs/changes/45/5345/4 && git cherry-pick FETCH_HEAD

# Apply necessary patches
# ---------------------------------------------------------------------------------

mkdir src/drivers/lenovo

touch src/drivers/lenovo/Kconfig
touch src/drivers/lenovo/Makefile.inc
touch src/drivers/lenovo/lenovo.h
touch src/drivers/lenovo/wacom.c

git add src/drivers/lenovo

# The above 4 files were all that were added on those changesets (5243/17, 5242/17 and 5239/19 from review.coreboot.org)
# I create empty versions of the files, and add them using "git add"
# Now I add the content to those files and all other changes, they will show up in a "git diff" which 
# was used to create the "gitdiff" file used below. This is also required for applying the gitdiff.

# THE ABOVE IS A HACK. "git add -N" I'm told can do the same thing as above, but that older versions of git do not have this.

# The changes below are in a patch at resources/libreboot/patch/gitdiff
# These were merged manually.
# Reason for manual merge: cherry pick wouldn't work with this branch. Didn't want to rebase just to scrap it later. Manual was easier.
# Also, some of the changes were for different code (eg the 3D fix was previously for the old native graphics code, not the new one).

# apply fix for 3D on kernel 3.12+ for X60/T60 on the new native graphics code (5320/9), 
# based on manually merging 5927/11 and 5932/5 (which fix the 3D on kernel 3.12+ on X60 for the *old* replay code) from review.coreboot.org
# apply fix for backlight controls for T60 and X60, based on docs/i945_backlight.html
# apply fix for IR/digitizer on X60/T60 and X60 Tablet (respectively), based on manually merging 5243/17, 5242/17 and 5239/19 from review.coreboot.org
# (see notes above)

# copy the gitdiff into coreboot directory
cp ../resources/libreboot/patch/gitdiff .

# apply it!
git apply gitdiff

# remove the copy (not needed anymore)
rm -rf gitdiff

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

# Apply coreboot-libre deblob script for coreboot git revision 63acd22dc5366c72a7165138f5030df9523824dc
# TODO: implement this.

# Deblobbing was done manually for this pre-release (will re-tool linux-libre deblob scripts later):
cd ../
echo "deblobbing coreboot"
./DEBLOB
echo "finished deblobbing coreboot"

# we're done
echo "FINISHED DOWNLOADING AND PATCHING COREBOOT"

# ------------------- DONE ----------------------