Received: from list by lists.gnu.org with archive (Exim 4.71) id 1aQFxO-000609-H1 for mharc-grub-devel@gnu.org; Mon, 01 Feb 2016 09:59:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41506) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aQFxJ-0005z3-3L for grub-devel@gnu.org; Mon, 01 Feb 2016 09:59:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aQFxF-0002Hf-Lh for grub-devel@gnu.org; Mon, 01 Feb 2016 09:59:53 -0500 Received: from sub5.mail.dreamhost.com ([208.113.200.129]:45552 helo=homiemail-a42.g.dreamhost.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aQFxF-0002HT-6z for grub-devel@gnu.org; Mon, 01 Feb 2016 09:59:49 -0500 Received: from homiemail-a42.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a42.g.dreamhost.com (Postfix) with ESMTP id 6F1D268C06B; Mon, 1 Feb 2016 06:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=shealevy.com; h=message-id :from:to:in-reply-to:subject:date:content-type:mime-version; s= shealevy.com; bh=GjQ05GAQGV0iRsxsfZQrQCrMCek=; b=Mf+BzoiKCUT5fIR ns06YCFZsRSf8t8GArW47J1LHRGL+Lus8f3BF0cM75pq6jTwJvgw0kGkFod4i3TC 47C+kNKfrXoZtcE8U6oE7ZPT14KnUVwwCngTgF5+YmDijLYIinDZKVQV0L4XYLpy WhLDS+CYRT4rk898S+a2sdl+WSgM= Received: from localhost (alc-nat.dreamhost.com [66.33.206.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: shea@shealevy.com) by homiemail-a42.g.dreamhost.com (Postfix) with ESMTPSA id 642BB68C05B; Mon, 1 Feb 2016 06:59:47 -0800 (PST) Message-Id: From: shea@shealevy.com To: "Shea Levy" , grub-devel@gnu.org X-Mailer: Atmail 7.5.3 X-Originating-IP: 73.186.74.217 in-reply-to: <024e397cbfee76eed796ac75b29c5fa5@shealevy.com> Subject: Re: [PATCH] Add module for interacting with the android bootloader control block Date: Mon, 01 Feb 2016 06:59:47 -0800 Content-Type: multipart/alternative; boundary="=_584d964c7dc52922b32cd3894a9d081e" MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Windows NT kernel [generic] [fuzzy] X-Received-From: 208.113.200.129 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Feb 2016 14:59:56 -0000 --=_584d964c7dc52922b32cd3894a9d081e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Actually, upon review my current needs technically only require read=0Af= unctionality (even though as I understand it the communication is=0Amean= t to be bidrectional), so if it would be easier to accept I can=0Aresubm= it a readonly version.=0A=0A----- Original Message -----=0AFrom: "Shea L= evy" =0ATo:=0ACc:=0ASent:Mon, 01= Feb 2016 09:27:38 -0500=0ASubject:Re: [PATCH] Add module for interactin= g with the android=0Abootloader control block=0A=0A Hi Andrei,=0A=0A The= re's no formal specification for any of this, all I have to go off=0A=0A= of is the bits of the android codebase I've read, but I'll try to=0Aans= wer =0A as best I can.=0A=0A On 2016-01-31 23:18, Andrei Borzenkov wrote= :=0A > 01.02.2016 02:55, Shea Levy =D0=BF=D0=B8=D1=88=D0=B5=D1=82:=0A >>= =0A >>=0A >> Android systems use a so-called "bootloader control block"= to=0A >> coordinate between the bootloader and the recovery system (whi= ch =0A >> handles=0A >> factory resets, system updates, firmware updates= , etc.). The BCB=0Ais=0A >> traditionally either an MTD device or an eMM= C partition, and it =0A >> contains=0A >> a struct bootloader_message di= rectly on the device/partition. The=0A >> recovery system will write a m= essage into the control block, which=0A=0A >> the=0A >> bootloader is th= en meant to check to see if it should boot into =0A >> recovery=0A >> mo= de or normal mode (the idea being that the control block is only=0A >> c= leared once recovery is finished, ensuring fault-tolerance; if=0Athe=0A= >> device is powered off mid-update, it will boot back into the same=0A= >> recovery mode).=0A >>=0A >> This module exports the bcb fields as en= v variables.=0A >> First the user sets android_bcb_disk to the partition= containing=0Athe =0A >> BCB=0A >> (I don't know how to support MTDs yet= but I only deal with x86). =0A >> Then=0A >> variables such as android_= bcb_command and android_bcb_recovery can=0A=0A >> be=0A >> checked and u= sed to determine which mode to boot into. They can =0A >> also, if=0A >>= necessary, be modified by setting the variables in question. The =0A >>= typical=0A >> use case will probably be to check if there is a recovery= command=0A >> present and if so boot into recovery mode.=0A >>=0A >> Do= es that explain enough?=0A >>=0A >=0A > How these variables are intended= to be used? Could you provide =0A > example=0A > of actual values in th= is block and example of grub code using them?=0A >=0A=0A The recovery se= rvice that's started when booted into recovery mode =0A reads values out= of the BCB to determine its operation. For example,=0Ait =0A checks tha= t the first (newline-separated) string in the 'recovery' is=0A=0A field= is "recovery", and then uses the rest to set its argv (e.g. a =0A recov= ery field consisting of "recoveryn--wipe_data" would specify a =0A facto= ry reset). The app itself writes in "boot-recovery" into the =0A "comman= d" field and copies over its arguments, so that upon reboot=0Athe =0A co= mmand will be started over again (until the command actually=0Afinishes,= =0A when it zeroes out the BCB).=0A=0A >=0A > How BCB area is identifie= d? Your code allows writing to arbitrary =0A > device=0A > without any s= anity check.=0A >=0A=0A On the android userspace side there's only a rec= overy.fstab file,=0Awhich =0A just names a block device. The code simply= reads a struct out of the =0A block with no magic number or other kind= of verification. Sometimes =0A there's not even a real partition table,= just a config file with an =0A offset into a block.=0A=0A >=0A > Is boo= tloader even *allowed* to make any choice here to remain=0A > compliant?= I.e. can bootloader ignore request stored in BCB?=0A >=0A=0A I have bee= n unable to find any formal spec on the matter, but at=0Aleast =0A respe= cting the "boot-recovery" command would probably be a good idea=0Afor= =0A stock android. But this seems more like policy than mechanism, and= =0AIMO =0A makes more sense to be determined at the grub.cfg level rathe= r than=0Athe =0A module level.=0A=0A ~Shea=0A=0A >=0A >>=0A >> Thanks,= =0A >> Shea=0A >>=0A >> On 2016-01-31 18:41, Vladimir 'phcoder' Serbinen= ko=0A >> wrote:=0A >>=0A >>> Sorry but I don't quite understand what thi= s module is for.=0A >> Can you please give an example of how it's used b= oth in original =0A >> context=0A >> and in GRUB. I have couple of style= comments as well but=0Aclarifying =0A >> the=0A >> usecase first is imp= ortant.=0A >>>=0A >>> Le lun. 1 f=C3=A9vr. 2016 00:01, Shea Levy=0A >> <= shea@shealevy.com> a =C3=A9crit :=0A >>>=0A >>>> ---=0A >>>> grub-core/M= akefile.core.def=0A >> | 5 ++=0A >>>> grub-core/android/android_bcb.c |= 163=0A >> ++++++++++++++++++++++++++++++++++++++++=0A >>>> 2 files chan= ged, 168=0A >> insertions(+)=0A >>>> create mode 100644 grub-core/androi= d/android_bcb.c=0A >>>>=0A >>=0A >>>> diff --git a/grub-core/Makefile.co= re.def=0A >> b/grub-core/Makefile.core.def=0A >>>> index 0cc40bb..b045ea= 4 100644=0A >>>> ---=0A >> a/grub-core/Makefile.core.def=0A >>>> +++ b/g= rub-core/Makefile.core.def=0A >>>> @@=0A >> -2327,3 +2327,8 @@ module= =3D {=0A >>>> common =3D loader/i386/xen_file64.c;=0A >>>>=0A >> extra_= dist =3D loader/i386/xen_fileXX.c;=0A >>>> };=0A >>>> +=0A >>>> +module= =3D {=0A >>>> +=0A >> name =3D android_bcb;=0A >>>> + common =3D androi= d/android_bcb.c;=0A >>>> +};=0A >>>> diff=0A >> --git a/grub-core/androi= d/android_bcb.c=0A >> b/grub-core/android/android_bcb.c=0A >>>> new file= mode 100644=0A >>>> index=0A >> 0000000..db49446=0A >>>> --- /dev/null= =0A >>>> +++=0A >> b/grub-core/android/android_bcb.c=0A >>>> @@ -0,0 +1,= 163 @@=0A >>>> +/*=0A >> android_bcb.c - module for interacting with the= android bootloader=0A >> control block */=0A >>>> +/*=0A >>>> + * GRUB= -- GRand Unified Bootloader=0A >>>> + *=0A >> Copyright (C) 2016 Free S= oftware Foundation, Inc.=0A >>>> + *=0A >>>> + * GRUB is=0A >> free soft= ware: you can redistribute it and/or modify=0A >>>> + * it under the=0A= >> terms of the GNU General Public License as published by=0A >>>> + *= the Free=0A >> Software Foundation, either version 3 of the License, or= =0A >>>> + * (at your=0A >> option) any later version.=0A >>>> + *=0A >>= >> + * GRUB is distributed in the hope=0A >> that it will be useful,=0A= >>>> + * but WITHOUT ANY WARRANTY; without even=0A >> the implied warra= nty of=0A >>>> + * MERCHANTABILITY or FITNESS FOR A=0A >> PARTICULAR PUR= POSE. See the=0A >>>> + * GNU General Public License for more=0A >> deta= ils.=0A >>>> + *=0A >>>> + * You should have received a copy of the GNU= =0A >> General Public License=0A >>>> + * along with GRUB. If not, see= =0A >> .=0A >>>> + */=0A >>>> +=0A >>>= > +#include=0A >> =0A >>>> +=0A >>>> +#include =0A= >>>> +#include =0A >>>>=0A >> +#include =0A >>= >> +=0A >>>> +GRUB_MOD_LICENSE ("GPLv3+");=0A >>>> +=0A >>>>=0A >> +/* D= efinition of struct bootloader message from=0A >> =0A >>=0Ahttps://andro= id.googlesource.com/platform/bootable/recovery/+/9d72d4175b06a70c64c8867= ff65b3c4c2181c9a9/bootloader.h#20=0A >> [2]=0A >>>> + * Available under= the following copyright and terms:=0A >>>> + *=0A >>>>=0A >> + * Copyri= ght (C) 2008 The Android Open Source Project=0A >>>> + *=0A >>>> + *=0A= >> Licensed under the Apache License, Version 2.0 (the "License");=0A >= >>> + *=0A >> you may not use this file except in compliance with the Li= cense.=0A >>>> + *=0A >> You may obtain a copy of the License at=0A >>>>= + *=0A >>>> + *=0A >> http://www.apache.org/licenses/LICENSE-2.0 [3]=0A= >>>> + *=0A >>>> + * Unless=0A >> required by applicable law or agreed= to in writing, software=0A >>>> + *=0A >> distributed under the License= is distributed on an "AS IS" BASIS,=0A >>>> + *=0A >> WITHOUT WARRANTIE= S OR CONDITIONS OF ANY KIND, either express or=0A >> implied.=0A >>>> += * See the License for the specific language governing=0A >> permissions= and=0A >>>> + * limitations under the License.=0A >>>> + */=0A >>>> +st= ruct=0A >> bootloader_message=0A >>>> +{=0A >>>> + char command[32];=0A= >>>> + char status[32];=0A >>>>=0A >> + char recovery[768];=0A >>>> + /= / The 'recovery' field used to be 1024=0A >> bytes. It has only ever=0A= >>>> + // been used to store the recovery command=0A >> line, so 768 by= tes=0A >>>> + // should be plenty. We carve off the last 256=0A >> bytes= to store the=0A >>>> + // stage string (for multistage packages) and=0A= >> possible future=0A >>>> + // expansion.=0A >>>> + char stage[32];=0A= >>>> + char=0A >> slot_suffix[32];=0A >>>> + char reserved[192];=0A >>>= > +} GRUB_PACKED;=0A >>>> +=0A >>>>=0A >> +struct bcb_state=0A >>>> +{= =0A >>>> + grub_disk_t disk;=0A >>>> + char=0A >> internal_write;=0A >>>= > + struct bootloader_message msg;=0A >>>> +};=0A >>>> +=0A >>>>=0A >> += static struct bcb_state state =3D { 0 };=0A >>>> +=0A >>>> +#define=0A >= > SET_FIELD(field)=0A >>>> + state.msg. field[sizeof state.msg. field -= 1] =3D=0A >> '';=0A >>>> + grub_env_set ("android_bcb_" #field, state.m= sg. field)=0A >>>> +=0A >>>>=0A >> +static void open_disk (const char *n= ame) {=0A >>>> + if (state.disk) {=0A >>>> +=0A >> grub_disk_close (stat= e.disk);=0A >>>> + }=0A >>>> +=0A >>>> + state.disk =3D=0A >> grub_disk_= open (name);=0A >>>> + if (state.disk) {=0A >>>> + grub_err_t err =3D=0A= >> grub_disk_read (state.disk, 0, 0, sizeof state.msg,=0A >>>> + &state= .msg);=0A >>>>=0A >> + if (err) {=0A >>>> + grub_disk_close (state.disk)= ;=0A >>>> + state.disk =3D 0;=0A >>>>=0A >> + } else {=0A >>>> + state.i= nternal_write =3D 1;=0A >>>> + SET_FIELD (command);=0A >>>> +=0A >> SET_= FIELD (status);=0A >>>> + SET_FIELD (recovery);=0A >>>> + SET_FIELD=0A >= > (stage);=0A >>>> + SET_FIELD (slot_suffix);=0A >>>> + state.internal_w= rite =3D 0;=0A >>>>=0A >> + }=0A >>>> + }=0A >>>> +}=0A >>>> +=0A >>>> += #define MAYBE_UPDATE_FIELD(field)=0A >>>> + do {=0A >> if (!grub_strcmp= (var->name, "android_bcb_" #field))=0A >>>> + {=0A >>>> +=0A >> grub_me= mcpy (state.msg. field, val, sizeof state.msg. field - 1);=0A >>>> +=0A= >> state.msg. field[sizeof state.msg. field - 1] =3D '';=0A >>>> +=0A >= > grub_disk_write (state.disk, 0,=0A >>>> + offsetof (struct=0A >> bootl= oader_message, field),=0A >>>> + sizeof state.msg. field, state.msg.=0A= >> field);=0A >>>> + } } while (0)=0A >>>> +=0A >>>> +static char *hand= le_write (struct=0A >> grub_env_var *var,=0A >>>> + const char *val)=0A= >>>> +{=0A >>>> + if (!grub_strcmp=0A >> (var->name, "android_bcb_disk"= ))=0A >>>> + {=0A >>>> + open_disk (val);=0A >>>> + if=0A >> (!state.dis= k)=0A >>>> + grub_print_error ();=0A >>>> + }=0A >>>> + else if (state.d= isk=0A >> && !state.internal_write)=0A >>>> + {=0A >>>> + MAYBE_UPDATE_F= IELD (command);=0A >>>> +=0A >> MAYBE_UPDATE_FIELD (status);=0A >>>> + M= AYBE_UPDATE_FIELD (recovery);=0A >>>> +=0A >> MAYBE_UPDATE_FIELD (stage)= ;=0A >>>> + MAYBE_UPDATE_FIELD (slot_suffix);=0A >>>> +=0A >> }=0A >>>>= +=0A >>>> + return grub_strdup (val);=0A >>>> +}=0A >>>> +=0A >>>>=0A >= > +GRUB_MOD_INIT(android_bcb)=0A >>>> +{=0A >>>> + const char *disk =3D= grub_env_get=0A >> ("android_bcb_disk");=0A >>>> + if (disk)=0A >>>> += {=0A >>>> + open_disk (disk);=0A >>>> +=0A >> if (!state.disk)=0A >>>>= + grub_print_error ();=0A >>>> + }=0A >>>> +=0A >>>> + if=0A >> (!grub_= register_variable_hook ("android_bcb_disk", 0,=0Ahandle_write))=0A >>>>= =0A >> + grub_print_error ();=0A >>>> + if (grub_register_variable_hook= =0A >> ("android_bcb_command", 0, handle_write))=0A >>>> + grub_print_er= ror ();=0A >>>> +=0A >> if (grub_register_variable_hook ("android_bcb_st= atus", 0,=0A >> handle_write))=0A >>>> + grub_print_error ();=0A >>>> += if=0A >> (grub_register_variable_hook ("android_bcb_recovery", 0,=0A >>= handle_write))=0A >>>> + grub_print_error ();=0A >>>> + if=0A >> (grub_= register_variable_hook ("android_bcb_stage", 0,=0Ahandle_write))=0A >>>>= =0A >> + grub_print_error ();=0A >>>> + if (grub_register_variable_hook= =0A >> ("android_bcb_slot_suffix", 0, handle_write))=0A >>>> + grub_prin= t_error=0A >> ();=0A >>>> +}=0A >>>> +=0A >>>> +GRUB_MOD_FINI(android_bc= b)=0A >>>> +{=0A >>>> +=0A >> grub_register_variable_hook ("android_bcb_= disk", 0, 0);=0A >>>> +=0A >> grub_register_variable_hook ("android_bcb_= command", 0, 0);=0A >>>> +=0A >> grub_register_variable_hook ("android_b= cb_status", 0, 0);=0A >>>> +=0A >> grub_register_variable_hook ("android= _bcb_recovery", 0, 0);=0A >>>> +=0A >> grub_register_variable_hook ("and= roid_bcb_stage", 0, 0);=0A >>>> +=0A >> grub_register_variable_hook ("an= droid_bcb_slot_suffix", 0, 0);=0A >>>> +=0A >>>> +=0A >> if (state.disk)= =0A >>>> + grub_disk_close (state.disk);=0A >>>> +}=0A >>>> --=0A >>>>= =0A >> 2.7.0=0A >>>>=0A >>>> ___________________________________________= ____=0A >>>>=0A >> Grub-devel mailing list=0A >>>> Grub-devel@gnu.org=0A= >>>>=0A >> https://lists.gnu.org/mailman/listinfo/grub-devel [4]=0A >>= =0A >>=0A >>=0A >>=0A >> Links:=0A >> ------=0A >> [1] http://www.gnu.or= g/licenses/=0A >> [2]=0A >> =0A >>=0Ahttps://android.googlesource.com/pl= atform/bootable/recovery/+/9d72d4175b06a70c64c8867ff65b3c4c2181c9a9/boot= loader.h#20=0A >> [3]=0A >> http://www.apache.org/licenses/LICENSE-2.0= =0A >> [4]=0A >> https://lists.gnu.org/mailman/listinfo/grub-devel=0A >>= =0A >>=0A >>=0A >> _______________________________________________=0A >>= Grub-devel mailing list=0A >> Grub-devel@gnu.org=0A >> https://lists.gn= u.org/mailman/listinfo/grub-devel=0A >>=0A >=0A >=0A > _________________= ______________________________=0A > Grub-devel mailing list=0A > Grub-de= vel@gnu.org=0A > https://lists.gnu.org/mailman/listinfo/grub-devel=0A=0A= _______________________________________________=0A Grub-devel mailing l= ist=0A Grub-devel@gnu.org=0A https://lists.gnu.org/mailman/listinfo/grub= -devel=0A --=_584d964c7dc52922b32cd3894a9d081e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Actually, upon review my current needs technically only require= read functionality (even though as I understand it the communication is= meant to be bidrectional), so if it would be easier to accept I can res= ubmit a readonly version.



----- Original Mess= age -----
From: "Shea Levy" <shea@shealevy.com>

To:
<grub-devel@gnu.org= >
Cc:

Sent:
Mon, 01 Feb 2= 016 09:27:38 -0500
Subject:
Re: [PATCH] Add module for interacting= with the android bootloader control block


=0AHi Andrei,
<= br>=0AThere's no formal specification for any of this, all I have to go= off
=0Aof is the bits of the android codebase I've read, but I'll t= ry to answer
=0Aas best I can.

=0AOn 2016-01-31 23:18, Andrei= Borzenkov wrote:
=0A> 01.02.2016 02:55, Shea Levy =D0=BF=D0=B8=D1= =88=D0=B5=D1=82:
=0A>>
=0A>>
=0A>> Android sy= stems use a so-called "bootloader control block" to
=0A>> coord= inate between the bootloader and the recovery system (which
=0A>&= gt; handles
=0A>> factory resets, system updates, firmware upda= tes, etc.). The BCB is
=0A>> traditionally either an MTD device= or an eMMC partition, and it
=0A>> contains
=0A>> a= struct bootloader_message directly on the device/partition. The
=0A&= gt;> recovery system will write a message into the control block, whi= ch
=0A>> the
=0A>> bootloader is then meant to check= to see if it should boot into
=0A>> recovery
=0A>> m= ode or normal mode (the idea being that the control block is only
=0A= >> cleared once recovery is finished, ensuring fault-tolerance; if= the
=0A>> device is powered off mid-update, it will boot back= into the same
=0A>> recovery mode).
=0A>>
=0A>&= gt; This module exports the bcb fields as env variables.
=0A>>= First the user sets android_bcb_disk to the partition containing the =0A>> BCB
=0A>> (I don't know how to support MTDs yet b= ut I only deal with x86).
=0A>> Then
=0A>> variables= such as android_bcb_command and android_bcb_recovery can
=0A>>= ; be
=0A>> checked and used to determine which mode to boot int= o. They can
=0A>> also, if
=0A>> necessary, be modifi= ed by setting the variables in question. The
=0A>> typical
= =0A>> use case will probably be to check if there is a recovery co= mmand
=0A>> present and if so boot into recovery mode.
=0A&g= t;>
=0A>> Does that explain enough?
=0A>>
=0A>= ;
=0A> How these variables are intended to be used? Could you prov= ide
=0A> example
=0A> of actual values in this block and ex= ample of grub code using them?
=0A>

=0AThe recovery service= that's started when booted into recovery mode
=0Areads values out o= f the BCB to determine its operation. For example, it
=0Achecks that= the first (newline-separated) string in the 'recovery' is
=0Afield= is "recovery", and then uses the rest to set its argv (e.g. a
=0Are= covery field consisting of "recovery\n--wipe_data\0" would specify a =0Afactory reset). The app itself writes in "boot-recovery" into the =0A"command" field and copies over its arguments, so that upon reboot= the
=0Acommand will be started over again (until the command actual= ly finishes,
=0Awhen it zeroes out the BCB).

=0A>
=0A&g= t; How BCB area is identified? Your code allows writing to arbitrary =0A> device
=0A> without any sanity check.
=0A>

= =0AOn the android userspace side there's only a recovery.fstab file, whi= ch
=0Ajust names a block device. The code simply reads a struct out= of the
=0Ablock with no magic number or other kind of verification.= Sometimes
=0Athere's not even a real partition table, just a config= file with an
=0Aoffset into a block.

=0A>
=0A> Is b= ootloader even *allowed* to make any choice here to remain
=0A> co= mpliant? I.e. can bootloader ignore request stored in BCB?
=0A>
=0AI have been unable to find any formal spec on the matter, but at= least
=0Arespecting the "boot-recovery" command would probably be a= good idea for
=0Astock android. But this seems more like policy tha= n mechanism, and IMO
=0Amakes more sense to be determined at the gru= b.cfg level rather than the
=0Amodule level.

=0A~Shea

= =0A>
=0A>>
=0A>> Thanks,
=0A>> Shea
=0A= >>
=0A>> On 2016-01-31 18:41, Vladimir 'phcoder' Serbinen= ko
=0A>> wrote:
=0A>>
=0A>>> Sorry but I d= on't quite understand what this module is for.
=0A>> Can you pl= ease give an example of how it's used both in original
=0A>> c= ontext
=0A>> and in GRUB. I have couple of style comments as we= ll but clarifying
=0A>> the
=0A>> usecase first is im= portant.
=0A>>>
=0A>>> Le lun. 1 f=C3=A9vr. 2016= 00:01, Shea Levy
=0A>> <shea@shealevy.com> a =C3=A9crit= :
=0A>>>
=0A>>>> ---
=0A>>>>= grub-core/Makefile.core.def
=0A>> | 5 ++
=0A>>>>= ; grub-core/android/android_bcb.c | 163
=0A>> +++++++++++++++++= +++++++++++++++++++++++
=0A>>>> 2 files changed, 168
= =0A>> insertions(+)
=0A>>>> create mode 100644 grub= -core/android/android_bcb.c
=0A>>>>
=0A>>
=0A= >>>> diff --git a/grub-core/Makefile.core.def
=0A>>= b/grub-core/Makefile.core.def
=0A>>>> index 0cc40bb..b04= 5ea4 100644
=0A>>>> ---
=0A>> a/grub-core/Makefi= le.core.def
=0A>>>> +++ b/grub-core/Makefile.core.def
= =0A>>>> @@
=0A>> -2327,3 +2327,8 @@ module =3D {=0A>>>> common =3D loader/i386/xen_file64.c;
=0A>>= >>
=0A>> extra_dist =3D loader/i386/xen_fileXX.c;
=0A&= gt;>>> };
=0A>>>> +
=0A>>>> +modu= le =3D {
=0A>>>> +
=0A>> name =3D android_bcb;=0A>>>> + common =3D android/android_bcb.c;
=0A>>= >> +};
=0A>>>> diff
=0A>> --git a/grub-cor= e/android/android_bcb.c
=0A>> b/grub-core/android/android_bcb.c=
=0A>>>> new file mode 100644
=0A>>>> inde= x
=0A>> 0000000..db49446
=0A>>>> --- /dev/null=0A>>>> +++
=0A>> b/grub-core/android/android_bcb= .c
=0A>>>> @@ -0,0 +1,163 @@
=0A>>>> +/*=0A>> android_bcb.c - module for interacting with the android bo= otloader
=0A>> control block */
=0A>>>> +/*
= =0A>>>> + * GRUB -- GRand Unified Bootloader
=0A>>&= gt;> + *
=0A>> Copyright (C) 2016 Free Software Foundation,= Inc.
=0A>>>> + *
=0A>>>> + * GRUB is
= =0A>> free software: you can redistribute it and/or modify
=0A&= gt;>>> + * it under the
=0A>> terms of the GNU General= Public License as published by
=0A>>>> + * the Free
= =0A>> Software Foundation, either version 3 of the License, or
= =0A>>>> + * (at your
=0A>> option) any later versio= n.
=0A>>>> + *
=0A>>>> + * GRUB is distrib= uted in the hope
=0A>> that it will be useful,
=0A>>&g= t;> + * but WITHOUT ANY WARRANTY; without even
=0A>> the imp= lied warranty of
=0A>>>> + * MERCHANTABILITY or FITNESS F= OR A
=0A>> PARTICULAR PURPOSE. See the
=0A>>>> += * GNU General Public License for more
=0A>> details.
=0A>= ;>>> + *
=0A>>>> + * You should have received a= copy of the GNU
=0A>> General Public License
=0A>>>= ;> + * along with GRUB. If not, see
=0A>> <http://www.gnu= .org/licenses/ [1]>.
=0A>>>> + */
=0A>>>&g= t; +
=0A>>>> +#include
=0A>> <stddef.h>=0A>>>> +
=0A>>>> +#include <grub/dl.h>= ;
=0A>>>> +#include <grub/env.h>
=0A>>>= >
=0A>> +#include <grub/disk.h>
=0A>>>>= +
=0A>>>> +GRUB_MOD_LICENSE ("GPLv3+");
=0A>>&g= t;> +
=0A>>>>
=0A>> +/* Definition of struct= bootloader message from
=0A>>
=0A>> https://android.= googlesource.com/platform/bootable/recovery/+/9d72d4175b06a70c64c8867ff6= 5b3c4c2181c9a9/bootloader.h#20
=0A>> [2]
=0A>>>>= + * Available under the following copyright and terms:
=0A>>&g= t;> + *
=0A>>>>
=0A>> + * Copyright (C) 2008= The Android Open Source Project
=0A>>>> + *
=0A>&g= t;>> + *
=0A>> Licensed under the Apache License, Version= 2.0 (the "License");
=0A>>>> + *
=0A>> you may= not use this file except in compliance with the License.
=0A>>= >> + *
=0A>> You may obtain a copy of the License at
= =0A>>>> + *
=0A>>>> + *
=0A>> http:/= /www.apache.org/licenses/LICENSE-2.0 [3]
=0A>>>> + *
= =0A>>>> + * Unless
=0A>> required by applicable law= or agreed to in writing, software
=0A>>>> + *
=0A>= > distributed under the License is distributed on an "AS IS" BASIS,=0A>>>> + *
=0A>> WITHOUT WARRANTIES OR CONDITION= S OF ANY KIND, either express or
=0A>> implied.
=0A>>&= gt;> + * See the License for the specific language governing
=0A&g= t;> permissions and
=0A>>>> + * limitations under the= License.
=0A>>>> + */
=0A>>>> +struct
= =0A>> bootloader_message
=0A>>>> +{
=0A>>&= gt;> + char command[32];
=0A>>>> + char status[32];=0A>>>>
=0A>> + char recovery[768];
=0A>>= >> + // The 'recovery' field used to be 1024
=0A>> bytes.= It has only ever
=0A>>>> + // been used to store the rec= overy command
=0A>> line, so 768 bytes
=0A>>>> += // should be plenty. We carve off the last 256
=0A>> bytes to= store the
=0A>>>> + // stage string (for multistage pack= ages) and
=0A>> possible future
=0A>>>> + // exp= ansion.
=0A>>>> + char stage[32];
=0A>>>>= + char
=0A>> slot_suffix[32];
=0A>>>> + char re= served[192];
=0A>>>> +} GRUB_PACKED;
=0A>>>&g= t; +
=0A>>>>
=0A>> +struct bcb_state
=0A>&= gt;>> +{
=0A>>>> + grub_disk_t disk;
=0A>>= >> + char
=0A>> internal_write;
=0A>>>> += struct bootloader_message msg;
=0A>>>> +};
=0A>>= ;>> +
=0A>>>>
=0A>> +static struct bcb_sta= te state =3D { 0 };
=0A>>>> +
=0A>>>> +#de= fine
=0A>> SET_FIELD(field)
=0A>>>> + state.msg.= field[sizeof state.msg. field - 1] =3D
=0A>> '';
=0A>>= ;>> + grub_env_set ("android_bcb_" #field, state.msg. field)
= =0A>>>> +
=0A>>>>
=0A>> +static void= open_disk (const char *name) {
=0A>>>> + if (state.disk)= {
=0A>>>> +
=0A>> grub_disk_close (state.disk);=
=0A>>>> + }
=0A>>>> +
=0A>>>&= gt; + state.disk =3D
=0A>> grub_disk_open (name);
=0A>>= ;>> + if (state.disk) {
=0A>>>> + grub_err_t err= =3D
=0A>> grub_disk_read (state.disk, 0, 0, sizeof state.msg,<= br>=0A>>>> + &state.msg);
=0A>>>>
=0A&= gt;> + if (err) {
=0A>>>> + grub_disk_close (state.dis= k);
=0A>>>> + state.disk =3D 0;
=0A>>>>=0A>> + } else {
=0A>>>> + state.internal_write= =3D 1;
=0A>>>> + SET_FIELD (command);
=0A>>>= > +
=0A>> SET_FIELD (status);
=0A>>>> + SET_F= IELD (recovery);
=0A>>>> + SET_FIELD
=0A>> (stag= e);
=0A>>>> + SET_FIELD (slot_suffix);
=0A>>>= > + state.internal_write =3D 0;
=0A>>>>
=0A>>= + }
=0A>>>> + }
=0A>>>> +}
=0A>>= >> +
=0A>>>> +#define MAYBE_UPDATE_FIELD(field)
= =0A>>>> + do {
=0A>> if (!grub_strcmp (var->name= , "android_bcb_" #field))
=0A>>>> + {
=0A>>>&= gt; +
=0A>> grub_memcpy (state.msg. field, val, sizeof state.ms= g. field - 1);
=0A>>>> +
=0A>> state.msg. field[= sizeof state.msg. field - 1] =3D '';
=0A>>>> +
=0A>= > grub_disk_write (state.disk, 0,
=0A>>>> + offsetof (= struct
=0A>> bootloader_message, field),
=0A>>>>= + sizeof state.msg. field, state.msg.
=0A>> field);
=0A>= >>> + } } while (0)
=0A>>>> +
=0A>>>= > +static char *handle_write (struct
=0A>> grub_env_var *var= ,
=0A>>>> + const char *val)
=0A>>>> +{=0A>>>> + if (!grub_strcmp
=0A>> (var->name, "a= ndroid_bcb_disk"))
=0A>>>> + {
=0A>>>> + o= pen_disk (val);
=0A>>>> + if
=0A>> (!state.disk)=
=0A>>>> + grub_print_error ();
=0A>>>> += }
=0A>>>> + else if (state.disk
=0A>> &&= ; !state.internal_write)
=0A>>>> + {
=0A>>>&g= t; + MAYBE_UPDATE_FIELD (command);
=0A>>>> +
=0A>&g= t; MAYBE_UPDATE_FIELD (status);
=0A>>>> + MAYBE_UPDATE_FI= ELD (recovery);
=0A>>>> +
=0A>> MAYBE_UPDATE_FIE= LD (stage);
=0A>>>> + MAYBE_UPDATE_FIELD (slot_suffix);=0A>>>> +
=0A>> }
=0A>>>> +
=0A= >>>> + return grub_strdup (val);
=0A>>>> +}=0A>>>> +
=0A>>>>
=0A>> +GRUB_MOD_= INIT(android_bcb)
=0A>>>> +{
=0A>>>> + con= st char *disk =3D grub_env_get
=0A>> ("android_bcb_disk");
= =0A>>>> + if (disk)
=0A>>>> + {
=0A>>= ;>> + open_disk (disk);
=0A>>>> +
=0A>> if= (!state.disk)
=0A>>>> + grub_print_error ();
=0A>&= gt;>> + }
=0A>>>> +
=0A>>>> + if
= =0A>> (!grub_register_variable_hook ("android_bcb_disk", 0, handle= _write))
=0A>>>>
=0A>> + grub_print_error ();=0A>>>> + if (grub_register_variable_hook
=0A>> ("= android_bcb_command", 0, handle_write))
=0A>>>> + grub_pr= int_error ();
=0A>>>> +
=0A>> if (grub_register_= variable_hook ("android_bcb_status", 0,
=0A>> handle_write))=0A>>>> + grub_print_error ();
=0A>>>> + if<= br>=0A>> (grub_register_variable_hook ("android_bcb_recovery", 0,<= br>=0A>> handle_write))
=0A>>>> + grub_print_error= ();
=0A>>>> + if
=0A>> (grub_register_variable_= hook ("android_bcb_stage", 0, handle_write))
=0A>>>>
= =0A>> + grub_print_error ();
=0A>>>> + if (grub_reg= ister_variable_hook
=0A>> ("android_bcb_slot_suffix", 0, handle= _write))
=0A>>>> + grub_print_error
=0A>> ();=0A>>>> +}
=0A>>>> +
=0A>>>>= +GRUB_MOD_FINI(android_bcb)
=0A>>>> +{
=0A>>>= ;> +
=0A>> grub_register_variable_hook ("android_bcb_disk",= 0, 0);
=0A>>>> +
=0A>> grub_register_variable_h= ook ("android_bcb_command", 0, 0);
=0A>>>> +
=0A>&g= t; grub_register_variable_hook ("android_bcb_status", 0, 0);
=0A>&= gt;>> +
=0A>> grub_register_variable_hook ("android_bcb_r= ecovery", 0, 0);
=0A>>>> +
=0A>> grub_register_v= ariable_hook ("android_bcb_stage", 0, 0);
=0A>>>> +
= =0A>> grub_register_variable_hook ("android_bcb_slot_suffix", 0, 0= );
=0A>>>> +
=0A>>>> +
=0A>> if (= state.disk)
=0A>>>> + grub_disk_close (state.disk);
= =0A>>>> +}
=0A>>>> --
=0A>>>><= br>=0A>> 2.7.0
=0A>>>>
=0A>>>> _____= __________________________________________
=0A>>>>
=0A= >> Grub-devel mailing list
=0A>>>> Grub-devel@gnu.o= rg
=0A>>>>
=0A>> https://lists.gnu.org/mailman/l= istinfo/grub-devel [4]
=0A>>
=0A>>
=0A>>
= =0A>>
=0A>> Links:
=0A>> ------
=0A>> [= 1] http://www.gnu.org/licenses/
=0A>> [2]
=0A>>
= =0A>> https://android.googlesource.com/platform/bootable/recovery/= +/9d72d4175b06a70c64c8867ff65b3c4c2181c9a9/bootloader.h#20
=0A>>= ; [3]
=0A>> http://www.apache.org/licenses/LICENSE-2.0
=0A&g= t;> [4]
=0A>> https://lists.gnu.org/mailman/listinfo/grub-de= vel
=0A>>
=0A>>
=0A>>
=0A>> ________= _______________________________________
=0A>> Grub-devel mailin= g list
=0A>> Grub-devel@gnu.org
=0A>> https://lists.gn= u.org/mailman/listinfo/grub-devel
=0A>>
=0A>
=0A>=0A> _______________________________________________
=0A> Gru= b-devel mailing list
=0A> Grub-devel@gnu.org
=0A> https://li= sts.gnu.org/mailman/listinfo/grub-devel


=0A__________________= _____________________________
=0AGrub-devel mailing list
=0AGrub-d= evel@gnu.org
=0Ahttps://lists.gnu.org/mailman/listinfo/grub-devel
=
--=_584d964c7dc52922b32cd3894a9d081e--