From patchwork Mon Apr 13 01:11:52 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 25885 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 1976ADEA84 for ; Mon, 13 Apr 2009 11:18:10 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B83B9DE142; Mon, 13 Apr 2009 11:16:02 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX18sbwY9m+zddE4RiDPFc3hkiyZLSGPNQ0I@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n3D1FqTZ007623 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 13 Apr 2009 01:15:52 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n3D1FqbF007622; Mon, 13 Apr 2009 01:15:52 GMT Message-Id: <20090413011137.725449532@am.sony.com> References: <20090413011136.475152916@am.sony.com> In-Reply-To: <20090413011136.475152916@am.sony.com> User-Agent: quilt/0.46-1 Date: Sun, 12 Apr 2009 18:11:52 -0700 From: Geoff Levand To: Jeremy Kerr Content-Disposition: inline; filename=ui-system-helpers.diff X-Virus-Scanned: ClamAV 0.93.3/9226/Sun Apr 12 20:02:45 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Mon, 13 Apr 2009 01:15:54 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 16/24] petitboot: Add ui-system helper routines X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Add some UI system helper routines: pb_run_kexec() pb_elf_hash() pb_cat_hash() pb_opt_hash() Signed-off-by: Geoff Levand --- rules.mk | 3 ui/common/ps3.c | 2 ui/common/ui-system.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++ ui/common/ui-system.h | 42 +++++++++++++ 4 files changed, 201 insertions(+), 2 deletions(-) --- a/rules.mk +++ b/rules.mk @@ -44,7 +44,8 @@ discover_objs = discover/udev.o discove discover/device-handler.o discover/paths.o discover/parser-utils.o # client objs -ui_common_objs = ui/common/discover-client.o ui/common/loader.o ui/common/url.o +ui_common_objs = ui/common/discover-client.o ui/common/loader.o \ + ui/common/ui-system.o ui/common/url.o ncurses_objs = twin_objs = ui/twin/pb-twin.o --- a/ui/common/ps3.c +++ b/ui/common/ps3.c @@ -28,7 +28,7 @@ #include #include "log/log.h" -#include "pb-system.h" +#include "ui-system.h" #include "ps3.h" static const char flash_dev[] = "/dev/ps3flash"; --- /dev/null +++ b/ui/common/ui-system.c @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * 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; version 2 of the License. + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "log/log.h" +#include +#include "talloc/talloc.h" +#include "loader.h" +#include "ui-system.h" + +/** + * run_kexec_local - Final kexec helper. + * @l_image: The local image file for kexec to execute. + * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL. + * @args: Optional command line args for kexec --append, can be NULL. + */ + +static int run_kexec_local(const char *l_image, const char *l_initrd, + const char *args) +{ + int result; + const char *argv[8]; + const char **p; + + p = argv; + *p++ = pb_system_apps.kexec; /* 1 */ + + if (l_initrd) { + *p++ = "--initrd"; /* 2 */ + *p++ = l_initrd; /* 3 */ + } + + if (args) { + *p++ = "--append"; /* 4 */ + *p++ = args; /* 5 */ + } + + /* First try by telling kexec to run shutdown */ + + *(p + 0) = l_image; + *(p + 1) = NULL; + + result = pb_run_cmd(argv); + + /* kexec will return zero on success */ + /* On error, force a kexec with the -f option */ + + if (result) { + *(p + 0) = "-f"; /* 6 */ + *(p + 1) = l_image; /* 7 */ + *(p + 2) = NULL; /* 8 */ + + result = pb_run_cmd(argv); + } + + if (result) + pb_log("%s: failed: (%d)\n", __func__, result); + + return result; +} + +/** + * pb_run_kexec - Run kexec with the supplied boot options. + * + * For the convenience of the user, tries to load both files before + * returning error. + */ + +int pb_run_kexec(const struct pb_kexec_data *kd) +{ + int result; + char *l_image; + char *l_initrd; + + pb_log("%s: image: '%s'\n", __func__, kd->image); + pb_log("%s: initrd: '%s'\n", __func__, kd->initrd); + pb_log("%s: args: '%s'\n", __func__, kd->args); + + if (kd->image) + l_image = pb_load_file(NULL, kd->image); + else { + l_image = NULL; + pb_log("%s: error null image\n", __func__); + } + + l_initrd = kd->initrd ? pb_load_file(NULL, kd->initrd) : NULL; + + if (!l_image || (kd->initrd && !l_initrd)) + result = -1; + else + result = run_kexec_local(l_image, l_initrd, kd->args); + + talloc_free(l_image); + talloc_free(l_initrd); + + return result; +} + +/** + * pb_elf_hash - Standard elf hash routine. + */ + +unsigned int pb_elf_hash(const char *str) +{ + unsigned int h = 0, g; + + while (*str) { + h = (h << 4) + *str++; + g = h & 0xf0000000; + if (g) + h ^= g >> 24; + h &= ~g; + } + pb_log("%s: %x\n", __func__, h); + return h; +} + +/** + * pb_cat_hash - Hashes concatenation of two strings. + */ + +unsigned int pb_cat_hash(const char *a, const char *b) +{ + unsigned int h; + char *s; + + s = talloc_asprintf(NULL, "%s%s", a, b); + h = pb_elf_hash(s); + talloc_free(s); + + return h; +} --- /dev/null +++ b/ui/common/ui-system.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * 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; version 2 of the License. + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if !defined(_PB_UI_SYSTEM_H) +#define _PB_UI_SYSTEM_H + +#include "pb-protocol/pb-protocol.h" +#include "system/system.h" + +struct pb_kexec_data { + char *image; + char *initrd; + char *args; +}; + +int pb_run_kexec(const struct pb_kexec_data *kd); + +unsigned int pb_elf_hash(const char *str); +unsigned int pb_cat_hash(const char *a, const char *b); + +static inline uint32_t pb_opt_hash(const struct device *dev, + const struct boot_option *opt) +{ + return pb_cat_hash(dev->name, opt->name); +} + +#endif